2012-10-10 55 views
1

這可能嗎?Linq to SQL加入IQueryable設置某些屬性

比方說,我有一個簡單的IQueryable加入象下面這樣:

  var x = from t1 in Repo.GetThing1() 
        join t2 in Repo.GetThing2() on t1.Key equals t2.Key 
        select t1).ToList(); 

但是,讓我們有,我想和T2設置在T1的字段。不過,我不想從T1領域重新映射,我只是希望將特定字段映射從T2 T1的能力...

回答

3

肯定的:

var query = from t1 in Repo.GetThing1() 
      join t2 in Repo.GetThing2() on t1.Key equals t2.Key 
      select new { Existing = t1, NewValue = t2.SomeField }; 

var list = query.ToList(); 

foreach (var pair in list) 
{ 
    pair.Existing.SomeField = pair.NewValue; 
} 

(然後你可以做任何你想要的,當然。)

+0

當然真棒! Thx老兄 – aherrick