简体   繁体   中英

Entity Framework Join Query based on SQL statement

I have SQL like this:

SELECT *  FROM [dbo].[INSTANCE] 
INNER JOIN dbo.TIME_INSTANCE on dbo.INSTANCE.INSTANCE_ID = dbo.TIME_INSTANCE.INSTANCE_ID
where customer_id=15 and TIME_ID = 1013

And I'm trying to use Entity framework to get this. I can pull back the instances from the instances table like this:

DBContext.Instances.Where(x => x.CustomerID == id && x.StartDateTime >= dateRange).OrderBy(x => x.StartDateTime).AsQueryable();

How do I perform the join like in the SQL above so that I do an inner join on the time_instance table?

quietgrit...I assume that your looking for a Lambda that will do the same thing?

I was able to mock this up and run it against my database. I copied the names of your objects into the below query. I used <theNameOfYourDataBase>Container instead of DbContext but the jest of the Lambda is the same. Let me know if this helps.

private myDbContainer db = new myDbContainer();

var myJoin = db.INSTANCE
.Join(db.TIME_INSTANCE , i => i.PK_Category, ti => ti.FK_INSTANCE, ((i, ti) => new { INSTANCE = i,TIME_INSTANCE  = ti }))
.Select(joinedStuff => new { 
joinedStuff.INSTANCE.customer_id,//Fill in the rest of the properties you want in your query 
joinedStuff.TIME_INSTANCE.TIME_ID  
}).Where(n => n.TIME_ID  == 1013 && n.customer_id == 15);//Add an .OrderBy() and or .GroupBy() 

Best Wishes, Bill

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM