Entity Framework LINQ joining 5 tables on multiple columns (impossible?!)

asp.net-mvc asp.net-mvc-4 entity-framework join linq-to-entities

Question

Please see image: enter image description here

I am trying to create a LINQ query that combines 5 different tables on 1 to 3 different columns. I am basically taking the examples of 2 tables and repeating the join sections for tables 3 through 5.

UPDATE: how do I get the following LINQ query to work?????

    var match = (from t1 in context.cKNA1
                 join t2 in context.cKNB1 on 
                        new { t1.KUNNR, t1.RowId } equals 
                        new { t2.KUNNR, t2.RowId }
                 join t3 in context.cKNVV on 
                        new { t2.KUNNR, t2.RowId } equals 
                        new { t3.KUNNR, t3.RowId }
                 join t4 in context.cKNVH on 
                        new { t3.KUNNR, t3.RowId } equals 
                        new { t4.KUNNR, t4.RowId }
        SELECT ....columns here....)

I can't find anything online where there are more than 3 tables *AND 2 or more columns.

1
2
8/13/2013 5:31:54 AM

Accepted Answer

Write this query:

 var match = from t1 in context.cKNA1
             join t2 in context.cKNB1 on 
                    new { t1.KUNNR, t1.RowId } equals 
                    new { t2.KUNNR, t2.RowId }
             join t3 in context.cKNVV on 
                    new { t2.KUNNR, t2.RowId } equals 
                    new { t3.KUNNR, t3.RowId }
             join t4 in context.cKNVH on 
                    new { t3.KUNNR, t3.RowId } equals 
                    new { t4.KUNNR, t4.RowId }
    SELECT t1.KUNNR;

In fact, just remove the parenthesis. If it still doesn't work, the visual studio will mark one of those join words as error. If so, it means that the columns you are trying to link, have different data types.

Suppose that the VS mark the first join word as error. So, it means that t1.KUNNR and t2.KUNNR have different data types, or t1.RowId and t2.RowId have different data types.

Check and correct your data types, the problem will go away.

3
7/19/2013 9:23:42 AM


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