Linq 表达式中的一个问题
这段代码有什么问题? 我在最后一行遇到了这个异常:
无法创建恒定值 类型 'System.Collections.Generic.IEnumerable`1'。 仅原始类型('如 Int32、 String 和 Guid') 支持 在此背景下。
var query = from colT in dal.TBL_Gharardad
select colT;
if(lstTarafeGharardadIds.Count>0)
query = from q in query
join id in lstTarafeGharardadIds on q.TarafeGharardadId equals id
select q;
dgvListeGharardad.DataSource = query.ToList();
lastTarafeGharardadIds 是一个 List
我还测试了
dgvListeGharardad.DataSource = query;
,则一切正常
query = from q in query
join id in lstTarafeGharardadIds on q.TarafeGharardadId equals id
select q;
如果 if
表达式等于 false 并且此代码无法运行 。但我无法理解我在最后一行(在此代码上)收到错误:
dgvListeGharardad.DataSource = query.ToList();
What is wrong with this code ?
I got this exception on the last line:
Unable to create a constant value of
type
'System.Collections.Generic.IEnumerable`1'.
Only primitive types ('such as Int32,
String, and Guid') are supported in
this context.
var query = from colT in dal.TBL_Gharardad
select colT;
if(lstTarafeGharardadIds.Count>0)
query = from q in query
join id in lstTarafeGharardadIds on q.TarafeGharardadId equals id
select q;
dgvListeGharardad.DataSource = query.ToList();
The lastTarafeGharardadIds is a List<int>
I also test
dgvListeGharardad.DataSource = query;
Everything works well if if
expression equals to false and this code
query = from q in query
join id in lstTarafeGharardadIds on q.TarafeGharardadId equals id
select q;
doesn't run. But I can't understand I got the error on the last line (on this code):
dgvListeGharardad.DataSource = query.ToList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 linq 无法在内存集合 (lstTarafeGharardadIds) 和数据库表 (dal.TBL_Gharardad、dal.v_Gharardad...) 之间进行连接。
类似的问题: 为什么这个 LINQ join 语句不起作用?
这个应该工作:
I think linq can't join between an in-memory collection (lstTarafeGharardadIds) and a database table (dal.TBL_Gharardad, dal.v_Gharardad...).
Similar problem: Why won't this LINQ join statement work?
This should work:
哇,这很难读!
无论如何,假设您的命名约定是正确的。你最终会得到:选择 colV。选择列会导致选择 IEnumerable,而不是数据源所需的原始值。
您可以尝试使用 SelectMany 来选择您需要的实际值
Wow, thats hard to read!
Anyways, assuming your naming convention is right. you end up with: select colV. Selecting a column results in selecting a IEnumerable rather then a primitive value which your dataSource requires.
You can try and use SelectMany to select the actual value you need