如何将包含联接的 linq-to-entities 查询绑定到可写 datagridview?

发布于 2024-10-11 02:09:33 字数 1453 浏览 3 评论 0原文

这个问题,虽然类似 其他,似乎不是重复的。如果是,请澄清,我很乐意合并。

我想使用包含联接的 linq-to-entities 查询绑定到可写 DataGridView。模型如下:

alt text

非规范化的 DataGridView 应该像这样绑定:

alt text

以下代码绑定但会生成只读 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:

alt text

The denormalized DataGridView should be bound like so:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-10-18 02:09:33

只需像这样定义演示类型即可。您不必在构造函数中传递对象:

public class LanguageDisplayTextPresentation 
{
    public int Key { get; set; }
    public string Text { get; set; }
    public string Language { get; set; }
}

然后

var query = from t in iDictionaryContext.DisplayTexts
    from l in iDictionaryContext.Languages
    where
        t.LanguageID == l.LanguageID
    select new LanguageDisplayTextPresentation 
    {
        Key = t.DisplayKey,
        Text = t.DisplayText1,
        Language = l.LanguageName
    };

Just define presentation type like that. You don't have to pass objects in constructor:

public class LanguageDisplayTextPresentation 
{
    public int Key { get; set; }
    public string Text { get; set; }
    public string Language { get; set; }
}

and then

var query = from t in iDictionaryContext.DisplayTexts
    from l in iDictionaryContext.Languages
    where
        t.LanguageID == l.LanguageID
    select new LanguageDisplayTextPresentation 
    {
        Key = t.DisplayKey,
        Text = t.DisplayText1,
        Language = l.LanguageName
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文