简体   繁体   English

加入LINQ中的匿名类型

[英]Join anonymous type in LINQ

i do have 2 c# declaration table , it column initialise during the creation of the program. 我确实有2个c#声明表,它在程序创建期间初始化列。

i wanted to join this table base on its UserID and UserName. 我想基于其UserID和UserName加入此表。

My Code is like following 我的代码如下

from nSJL in UserList.AsEnumerable()
join SJL in UserListOnline.AsEnumerable()
on
new { nSJL.Field<int>("UserID"), nSJL.Field<string>("UserName") }
equals
new { nSJL.Field<int>("UserID"), nSJL.Field<string>("UserName") }
into sjList

in this code i am getting the error 在这段代码中我得到了错误

Invalid anonymous type member declarator. 无效的匿名类型成员声明符。 Anonymous type members must be declared with a member assignment, simple name or member access. 必须使用成员分配,简单名称或成员访问声明匿名类型成员。

Anyway to join anonymous type? 无论如何加入匿名类型?

You need to specify the names for the anonymous type properties: 您需要指定匿名类型属性的名称:

from nSJL in UserList.AsEnumerable()
join SJL in UserListOnline.AsEnumerable()
on
new { UserID = nSJL.Field<int>("UserID"),
      UserName = nSJL.Field<string>("UserName") }
equals
new { UserId = SJL.Field<int>("UserID"),
      UserName = SJL.Field<string>("UserName") }
into sjList

Note that I've also changed the right hand side of the join to use SJL rather than nSJL too, as otherwise it's invalid. 请注意,我也改变了连接的右侧使用SJL而不是nSJL ,否则它无效。 It would help your code's clarity if you'd use rather more meaningful names though... 如果你使用更有意义的名字,这将有助于你的代码清晰......

from nSJL in UserList.AsEnumerable()
join SJL in UserListOnline.AsEnumerable()
on
new{  UserID = nSJL.Field<int>("UserID"), UserName = nSJL.Field<string>("UserName") }
equals
new { UserID = nSJL.Field<int>("UserID"), UserName = nSJL.Field<string>("UserName") } into sjList

You weren't declaring the field names for your anonymous type. 您没有声明匿名类型的字段名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM