简体   繁体   English

基于SQL语句的实体框架联接查询

[英]Entity Framework Join Query based on SQL statement

I have SQL like this: 我有这样的SQL:

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? 我如何像上面的SQL中那样执行联接,以便对time_instance表进行内部联接?

quietgrit...I assume that your looking for a Lambda that will do the same thing? quietgrit ...我假设您正在寻找可以做相同事情的Lambda吗?

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. 我使用了<theNameOfYourDataBase> Container而不是DbContext,但是Lambda的笑话是相同的。 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 最好的祝愿,比尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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