Join anonymous type in LINQ

I have 2 C # declaration tables, it initializes the column during program creation.

I wanted to join this table base in UserID and UserName.

My code is like the 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 get an error

Invalid member declarator of anonymous type. Members of an anonymous type must be declared with the appointment of members, a simple name, or access to a member.

Anyway to join an anonymous type?

+6
source share
2 answers

You need to specify property names of an anonymous type:

 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 also changed the right side of the connection to use SJL , not nSJL , since otherwise this is not valid. This will help improve code clarity if you use more meaningful names, though ...

+13
source
 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 did not declare field names for your anonymous type.

+5
source

Source: https://habr.com/ru/post/914010/


All Articles