2012-05-30 69 views
0

我有以下查詢LINQ INNER JOIN - 返回兩個表

var customers = from customer in context.tblAccounts 
       join assoc in context.tblAccountAssociations on customer.AccountCode equals assoc.ChildCode 
       where customer.AccountType == "S" || customer.AccountType == "P" 
       select customer, assoc; 

C#不喜歡 「ASSOC」 結尾。

我的錯誤信息是:

名爲「assoc命令」的局部變量不能在此範圍內聲明,因爲它會給予不同的意義「assoc命令」,這已經是一個「孩子」範圍用來表示別的東西。

我需要從兩個表中返回所有列,然後用

的foreach(在客戶VAR的客戶)

+3

難道你不能......改變變量的名字? – GregRos

+0

您需要返回2個單獨的對象還是需要填充客戶對象的屬性? –

回答

7

迭代爲什麼你有這樣一行:

select customer, assoc; 

你是不是想返回一個客戶,一個關聯,或兩者?假如是後者,你可以使用匿名類型將它們組合起來:

select new { Customer = customer, Assoc = assoc }; 

然後在customers每個項目將有兩個屬性,CustomerAssoc,你可以抓住你要麼需要什麼。

+0

非常感謝! – dorkboy

0

您可以用匿名類型包裝這兩個項目。

var customers = from customer in context.tblAccounts 
      join assoc in context.tblAccountAssociations on customer.AccountCode equals assoc.ChildCode 
      where customer.AccountType == "S" || customer.AccountType == "P" 
      select new {customer, assoc};