2013-01-08 144 views
1

我有一个Linq查询返回三个数据元素。Linq查询,返回不同的单个字段和返回的数据子集

var billingDateResults = from s in Subscriptions 
      .Where(s => (s.ProductCode.Contains("myCode"))) 
     select { g.ID, BillingDate =s.BILL_THRU, s.ProductCode}; 

我想对此做不同类型的查询,以将结果限制为每个ID一条记录。

var billingDateResults = from s in Subscriptions 
      .Where(s => (s.ProductCode.Contains("myCode"))) 
group s by s.ID into g 
select g.FirstOrDefault(); 

这工作,但现在返回的所有字段中的记录,我想通过限制的结果,只有在第一个例子中的3场,以数据的量最小化。

这样做的好方法是什么?

TIA

回答

2

然后按这三个字段分组。

var billingDateResults = 
    from s in Subscriptions 
    where s.ProductCode.Contains("myCode") 
    group new 
    { 
     g.ID, 
     BillingDate = s.BILL_THRU, 
     s.ProductCode 
    } by s.ID into g 
    select g.First(); // FirstOrDefault is not necessary, the groups will be non-empty 
+0

First()就足够了 – Tilak