开发者

How do you bind a linq-to-entities query that contains a join to a writable datagridview?

开发者 https://www.devze.com 2023-02-03 10:00 出处:网络
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.

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:

How do you bind a linq-to-entities query that contains a join to a writable datagridview?

The denormalized DataGridView should be bound like so:

How do you bind a linq-to-entities query that contains a join to a writable datagridview?

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


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
    };
0

精彩评论

暂无评论...
验证码 换一张
取 消