What's the simplest way to do a Linq join query ? RRS feed

  • Question

  • right now my query in c# looks like this,
    Table1Entry bMfi = MyDB.Table1.Single(x => x.Name == "abc");
    Table1 is like this {Name, field1, field2}.

    now i break Table1 into 2 tables,
    Table0 has {id, Name};
    Table1 has {id, field1, field2}.

    What's the easiest way to do a query based on Name now ?
    Sunday, August 11, 2013 10:38 PM


  • Hi ningji;

    I am going to assume that there is a foreign key association between the two tables. The field in table t1, fid, Is the foreign key to table T0. Also the results in bMfi will be of an anonymous type because of the joining of the two tables. If you create a class to hold the fields of the results set then you can assign it to a return type of that class type.

    var bMfi = MyDB.Table0.Join(Table1, t0 =>, t1 => t1.fid, (t0, t1) => new {, t0.Name, t1.field1, t1.field2}).Single( x => x.Name == "abc"); 


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by ningji Monday, August 12, 2013 3:11 AM
    Monday, August 12, 2013 1:59 AM