2013-06-12 62 views
0

我正在使用Linq to Sql獲取兩個列表,而不是使用另一個列表的聯合。他們工作正常。但我想知道是否可以在一個查詢或兩個而不是我擁有的三個查詢中完成。組合Querys

Querys是

var l = (from t in T_list1 
     where t.Date == DateTime.Today 
     select new 
     { 
      oldDate=t.OldDate, 
      Name=t.name, 
      Email=t.EmailAddress, 
      list2TableId=t.l2Id, 
      CustomerId=t.customerId 
     }); 


var l2=(from d in T_list2 
     from e in l1 
     where d.Id == e.list2TableId 
     select new 
     {     
      Date=d.oldDate, 
      CName=d.Name, 
      Experience=e.experience,    
     }); 

list2.Dump(); 
var l3 = list2.Union(list3).ToList(); 

我一直在尋找這個職位,但沒有工作。 Combining 2 Linq queries into 1 感謝您的輸入。

+0

你的代碼有什麼問題,你爲什麼要改變它? –

+0

我懷疑有些拼寫錯誤,但發佈的代碼沒什麼意義。請檢查l/l1/list1和l2/list2 –

回答

0

你可以做一個連接,而不是2個查詢:

  var l = (from t in T_list1 
        join d in T_list2 on t.l2Id equals d.Id 
       where t.Date == DateTime.Today 
       select new 
       { 
       oldDate=t.OldDate, 
       Name=t.name, 
       Email=t.EmailAddress, 
       list2TableId=t.l2Id, 
       CustomerId=t.customerId 
       Date=d.oldDate, 
       CName=d.Name 
       Experience=e.experience, 
       }); 

我沒有測試過的查詢,但它應該告訴你的粗略的想法。

查看SQL中的JOIN & Linq以獲取更多信息。