在LINQ中加入匿名类型 [英] Join anonymous type in LINQ

查看:120
本文介绍了在LINQ中加入匿名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实有2个c#声明表,它在程序创建期间初始化列.

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.

我的代码就像下面的

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

请注意,我还更改了连接的右侧,也使用了SJL而不是nSJL,否则它是无效的.如果您使用更有意义的名称,则将有助于代码的清晰度.

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...

这篇关于在LINQ中加入匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆