LINQ内部联接-从两个表中返回 [英] LINQ Inner Join - Return From Both Tables
问题描述
我有以下查询
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#不喜欢结尾处的关联".
C# does not like the "assoc" at the end.
我的错误消息是:
不能在此范围内声明名为"assoc"的局部变量,因为它将赋予"assoc"不同的含义,"assoc"已在子"范围中用于表示其他内容.
A local variable named 'assoc' cannot be declared in this scope because it would give a different meaning to 'assoc', which is already used in a 'child' scope to denote something else.
我需要从两个表中返回所有列,然后使用
I need to return all columns from both table and then iterate with a
foreach(客户中的固定客户)
foreach (var customer in customers)
推荐答案
为什么要显示以下行:
select customer, assoc;
您是要回头客还是要回头客,或者两者兼而有之?假设是后者,则可以使用匿名类型将它们组合起来:
Are you trying to return a customer, an assoc, or both? Assuming the latter, you can combine them using anonymous types:
select new { Customer = customer, Assoc = assoc };
然后customers
中的每个项目都将具有两个属性,分别为Customer
和Assoc
,您可以从任一属性中获取所需的内容.
Then each item in customers
would have two properties, Customer
and Assoc
and you can grab what you need from either.
这篇关于LINQ内部联接-从两个表中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!