How do I use Linq and Entity Framework to join two jointables?

前端 未结 2 591
情歌与酒
情歌与酒 2021-01-22 21:11

I have a very normalized database, and I\'m trying to join two join tables together.

My goal is to only show documents that the user has permission to. I\'m using Ent

2条回答
  •  甜味超标
    2021-01-22 21:51

    You can also view the SQL generated by EF 4.1 using either

    • SQL Profiler
    • LINQPad

    Although, at this time, you need to use the LINQPad beta for EF 4.1.

    Regarding your second question, I believe your query would translate fine. Using LINQPad to check the SQL, the following query

    var a1 = Addresses.Where(a => a.City.ToUpper().EndsWith("L")).Select(a => a.AddressID);
    var a2 = Addresses.Where(a => a.City.ToUpper().StartsWith("B")).Select(a => a.AddressID);
    
    var x1 = a1.Intersect(a2);
    

    translates to

    SELECT 
    [Intersect1].[AddressID] AS [C1]
    FROM  (SELECT 
        [Extent1].[AddressID] AS [AddressID]
        FROM [Person].[Address] AS [Extent1]
        WHERE UPPER([Extent1].[City]) LIKE N'%L'
    INTERSECT
        SELECT 
        [Extent2].[AddressID] AS [AddressID]
        FROM [Person].[Address] AS [Extent2]
        WHERE UPPER([Extent2].[City]) LIKE N'B%') AS [Intersect1]
    

    I think @Slauma's recommendation to use navigation proeprties is the way to go if your model supports it.

    Still, get LINQPad - you won't regret it :)

提交回复
热议问题