2013-01-08 74 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