0

I have a need to get 2 record types from a join out of LINQ. I would prefer not to create a single amalgamated type out of the join because they both have about 2 dozen columns and I already have MVC model types defined to received types from each of the two tables.

So far, I have something like this:

var qry = from recT1 in db.Tbl1
          join recT2 in db.Tbl2 on recT1.colA equals recT2.colA
          where recT1.colA != null
          select new { MyT1 = recT1, MyT2 = recT2 };

Then, I was trying to do something slick like:

public class T1and2
{
    public T1Model T1 { get; set; }
    public T2Model T2 { get; set; }
}

T1and2[] aT12 = qry.Select( obj => new T1and2()
    {
        T1 = new T1Model(obj.MyT1),    //-- these ctors defined separately
        T2 = new T2Model(obj.MyT2)
    })
    .ToArray();

But this gives me a runtime NotSupportedException stating "Only parameterless constructors are supported."

My next step will be to simply create a for-loop to extract the model classes. I was just trying to find out if there is a better way to make the transfer.

ADD: To add more clarity, I need to transfer the data from the recT? types to the T?Model types. The T?Model types are model types that are adorned for MVC data display and formatting. And one of the big things I'm trying to avoid is the same long list of property assignments at multiple places in the code.

3
  • Thanks, Yuriy. But I don't think this is any different from the anonymous object currently in use. The anonymous object works fine for nesting multiple objects coming out of the query. It's translating from the entity types to my model types where the problem seems to be occurring. Let me know if I'm misreading your suggesting.
    – Askable
    Dec 18, 2012 at 16:36
  • I just saw T1and2 and thought of Tuple. Dec 18, 2012 at 17:16

2 Answers 2

0

Try this

var aT12 =
    from obj in qry
    let t1 = new T1Model(obj.MyT1)
    let t2 = new T2Model(obj.MyT2)
    select mew T1and2 { T1 = t1, T2 = t2 };
1
  • I should have been more clear on the types used. I don't want the "entity" types coming from the table definition. I want the data transferred into my types that were designed to be exposed as model objects. The model types have been adorned with display attributes for formatting and labels. I'm trying to transfer the data from the two entity types to the two model types, but without explicitly assigning all the properties. I created the ctors to do the transfer so I can reuse that logic and not have a bunch of different places where I have the same long list of fields being transferred.
    – Askable
    Dec 18, 2012 at 16:27
0

It seems to me that you can do this as well:

var aT12 = qry.Select( obj => new T1and2()
    {
        T1 = obj.MyT1,
        T2 = obj.MyT2
    })
    .ToArray();

I don't know if the constructors contain any processing logic. If so, you could defer that processing to the moment that MyT1 or MyT2 are actually read in your for loop.

3
  • I should have been more clear on the types used. I don't want the "entity" types coming from the table definition. I want the data transferred into my types that were designed to be exposed as model objects. The model types have been adorned with display attributes for formatting and labels. I'm trying to transfer the data from the two entity types to the two model types, but without explicitly assigning all the properties. I created the ctors to do the transfer so I can reuse that logic and not have a bunch of different places where I have the same long list of fields being transferred.
    – Askable
    Dec 18, 2012 at 16:27
  • Well, you could transfer the constructor logic to the setters of the properties and make the properties set-only. Two times a helluva anti pattern, but maybe condonable because the requirement of parameterless constructors forced you to do it. Dec 18, 2012 at 16:35
  • Yeah. But if I was forced to go in that direction, it would just be easier to use a for-loop. I was really just trying to figure out if there was a slick way (one line) to do the conversion that I'm not aware of. This is starting to look like a hard limitation.
    – Askable
    Dec 18, 2012 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.