简体   繁体   中英

Join anonymous type in LINQ

i do have 2 c# declaration table , it column initialise during the creation of the program.

i wanted to join this table base on its UserID and 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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