3

I am using Entity Framework. I want to select data from multiple tables in the database having no foreign key relation like

select  
    tOut.columnId, wo.columnType, tIn.* 
from 
    TbaleA tIn,
    TableB tOut,
    TableC wo
where
    1 = 1
    and tIn.columnRefId = tOut.columnGuid
    and tOut.columnId = wo.columnId

Is there any solution in Entity Framework for this? I have tried by using include syntax its not working for me ..

1
  • What is not working? Please be more specific. Commented Jan 6, 2015 at 12:21

3 Answers 3

3

You can join tables even though no foreign key relation exists if you use the query syntax. It would go something along these lines:

var result = from tIn in yourDbContext.TableA
             join tOut in yourDbContext.TableB on tIn.columnRefId equals tOut.columnGuid
             join wo in yourDbContext.TableC on tOut.columnId equals wo.columnId
             select new { tOut.columnId, wo.columnType, TableA = tIn };
Sign up to request clarification or add additional context in comments.

1 Comment

it will work, but imho it is an "underutilization" of the ORM. Setting navigation properties seems more in the spririt that just mimic SQL statement.
0

There are examples for both lambda and query syntax on these links:

MSDN 101 LINQ Samples (mostly query syntax)

Nilzor's LINQ 101 Samples - Lambda Style

Comments

0

Another solution is to use/add navigation properties to your classes, as for example:

class TableA {
    Int32 Id {get; set;}

    //navigation property
    TableB b {get; set;}
}

and use them in the query

from a in yourDbContext.TableA
select new { a.Id, a.b.columnType}

Of course there may be a little configuration (code or annotation) to setup the relations to make them fit the database schema. But you will do this effort once and not for every query.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.