ADO.NET + C# + LINQ Join two tables using EF and return everything without having to define all fields explicitly.

ado.net c# entity-framework linq

Question

I have the following straightforward LINQ query on ADO.NET Entity Framework.

var result = 
  from itemA in TableA
  join itemB in TableB on itemA.ID = itemB.ID
  select ??????

I'm making an effort to choose everything fromitemA and itemB without listing everyTableA and TableB .

anything to consider?

1
4
4/6/2010 9:53:54 PM

Accepted Answer

Do you really need this?

    var result = from itemA in TableA 
                 join itemB in TableB on itemA.ID equals itemB.ID
                 select new { itemA, itemB };

You may also create a result class that aids in creating the result object without requiring you to describe every field:

    class ItemAB
    {
        public ItemAB(ItemA a, ItemB b)
        {
            FieldA1 = a.FieldA1;
            FieldA2 = a.FieldA2;
            FieldB1 = b.FieldB1;
            FieldB2 = b.FieldB2;

        }
        public int FieldA1 { get; private set; }
        public int FieldA2 { get; private set; }
        public int FieldB1 { get; private set; }
        public int FieldB2 { get; private set; }
    }

    var result = from itemA in TableA 
                 join itemB in TableB on itemA.ID equals itemB.ID
                 select new ItemAB(itemA, itemB);
6
7/13/2009 5:07:57 PM

Popular Answer

It seems that you have two tables with a one-to-one connection based on your link query.

If so, you should set up your entity model such that two tables are combined into a single entity. View details at:

http://blogs.msdn.com/simonince/archive/2009/03/23/mapping-two-tables-to-one-entity-in-the-entity-framework.aspx



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow