获取两个Data表中的公共行

本文关键字:Data 两个 获取 | 更新日期: 2023-09-27 17:54:23

我有一个表PromotionRequest,有4列:

| PromoId | PromoType 
----------------------
|   101   |  1   |  
|   101   | 2    |  
|   103   | 3    |  
|   104   | 4    |  

我有一个表PromotionResponsek有4列(PromoId, PromoType, Value, PromoDesc),其中一个是PromoId

我必须找到PromotionResponsePromoId值与PromotionRequest表的PromoId值匹配的所有行。

获取两个Data表中的公共行

你已经用SQL标记了这个,所以如果你想在SQL中执行这个,那么你将使用INNER JOIN:

select rs.promoid,
  rs.promotype,
  rs.value,
  rs.promodesc
from PromotionResponse rs
inner join PromotionRequest pr
  on rs.promoid = pr.promoid

您可以尝试使用这种linq方法在响应表中查找id,如下所示

List<int> ids = requesttable.AsEnumerable()
               .Select(r => (int) r["Id"])
               .ToList();

var query = from item in responsetable.AsEnumerable()
            where ids.Contains( (int) item["Id"] )
            select item;
var matched = from table1 in PromotionRequest.AsEnumerable()
               join table2 in PromotionResponse .AsEnumerable() on
               table1.Field<string>("PromoId ") equals table2.Field<string>("PromoId ")
               where table1.Field<string>("PromoId ") == table2.Field<string>("PromoId ")
               select table2;
if (matched.Any())
 {
 }