0

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

Community
  • 1
  • 1
Tim Partridge
  • 3,365
  • 1
  • 42
  • 52

1 Answers1

1

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
    };
LukLed
  • 31,452
  • 17
  • 82
  • 107
  • 1
    Although this will make the DataGridView editable, this won't update the entities, will it? And how about adding/deleting rows? – Kay Zed Feb 04 '11 at 16:04