如何将包含联接的 linq-to-entities 查询绑定到可写 datagridview?
这个问题,虽然类似 至 其他,似乎不是重复的。如果是,请澄清,我很乐意合并。
我想使用包含联接的 linq-to-entities 查询绑定到可写 DataGridView。模型如下:
非规范化的 DataGridView 应该像这样绑定:
以下代码绑定但会生成只读 DataGridView,因为 linq-to-entities 查询返回匿名类型 (查看这篇文章)。我陷入了僵局,因为我认为我需要匿名类型来进行非规范化。
var query = from t in iDictionaryContext.DisplayTexts
from l in iDictionaryContext.Languages
where
t.LanguageID == l.LanguageID
select new
{
Key = t.DisplayKey,
Text = t.DisplayText1,
Language = l.LanguageName
};
我还尝试了此处建议的解决方案,但是它似乎适用于 linq-to-sql 但不适用于 linq-to-entities。将 bindingsource.datasource 设置为 linq-toentities 查询时,会引发异常,显示“LINQ to Entities 仅支持无参数构造函数和初始化程序”。
谢谢你的建议,
蒂姆
This question, although similar to others, doesn't seem to be a duplicate. If it is, please clarify and I will be happy to merge.
I want to bind to a writable DataGridView using a linq-to-entities query containing a join. The model is as follows:
The denormalized DataGridView should be bound like so:
The following code binds but results in a readonly DataGridView because the linq-to-entities query returns an anonymous type (see this post). I'm at an impasse because I think I need the anonymous type to do the denormalization.
var query = from t in iDictionaryContext.DisplayTexts
from l in iDictionaryContext.Languages
where
t.LanguageID == l.LanguageID
select new
{
Key = t.DisplayKey,
Text = t.DisplayText1,
Language = l.LanguageName
};
I also tried the solution suggested here but it seems to apply to linq-to-sql but not to linq-to-entities. When setting the bindingsource.datasource to the linq-to-entities query, an exception is thrown reading "Only parameterless constructors and initializers are supported in LINQ to Entities."
Thank you for your advice,
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需像这样定义演示类型即可。您不必在构造函数中传递对象:
然后
Just define presentation type like that. You don't have to pass objects in constructor:
and then