[英]How do I do a multi-join on two SQL Views in an Entity Framework Model?
I have two views in my model. 我的模型中有两个视图。
I basically need to do an INNER JOIN on them based on three columns: 我基本上需要基于三列对它们进行INNER JOIN:
The first thing I don't know how to do is add the SQL "AND" operator to the LINQ expression. 我不知道怎么做的第一件事是将SQL“ AND”运算符添加到LINQ表达式中。
The second thing is, I don't know how to SELECT the JOINED table. 第二件事是,我不知道如何选择JOINED表。
Can someone give me a hand? 有人可以帮我吗?
var query = (from s in db.TVData_VW_ShowList
from z in db.TVData_VW_Schedule
where s.dataSource = z.dataSource
&& s.ShowID = z.ShowID
&& s.EpisodeId = z.EpisodeId select ...
You can use anonymous types to your advantage here, both to join across multiple columns, and to project into a new type containing data from both sides of the join. 您可以在这里使用匿名类型,以在多个列之间进行连接,以及将其投影到包含来自连接双方的数据的新类型中,从而发挥自己的优势。 Here's a working example using Linq to objects:
这是一个使用Linq来处理对象的工作示例:
namespace LinqExample
{
class Program
{
static void Main()
{
var Shows = new List<ShowData> { new ShowData { dataSource = "foo", EpisodeID = "foo", ShowID = "foo", SomeShowProperty = "showFoo" }};
var Schedules = new List<ScheduleData> { new ScheduleData { dataSource = "foo", EpisodeID = "foo", ShowID = "foo", SomeScheduleProperty = "scheduleFoo" } };
var results =
from show in Shows
join schedule in Schedules
on new { show.dataSource, show.ShowID, show.EpisodeID }
equals new { schedule.dataSource, schedule.ShowID, schedule.EpisodeID }
select new { show.SomeShowProperty, schedule.SomeScheduleProperty };
foreach (var result in results)
{
Console.WriteLine(result.SomeShowProperty + result.SomeScheduleProperty); //prints "showFoo scheduleFoo"
}
Console.ReadKey();
}
}
public class ShowData
{
public string dataSource { get; set; }
public string ShowID { get; set; }
public string EpisodeID { get; set; }
public string SomeShowProperty { get; set; }
}
public class ScheduleData
{
public string dataSource { get; set; }
public string ShowID { get; set; }
public string EpisodeID { get; set; }
public string SomeScheduleProperty { get; set; }
}
}
So to join you can use the join
keyword then use on
to specify the conditions. 因此,要加入,可以使用
join
关键字,然后使用on
指定条件。 &&
(the logical and operator in C#) will be translated to the SQL AND
keyword. &&
(C#中的逻辑和运算符)将转换为SQL AND
关键字。
Also, in EF they have what are known as "implicit joins" meaning if I have TableA with a foreign key to TableB, call it fKey. 另外,在EF中,它们具有所谓的“隐式连接”,这意味着如果我的TableA带有TableB的外键,则将其称为fKey。
Doing where TableA.fKey == TableB.pKey
will cause the provider to put a join there. 在
TableA.fKey == TableB.pKey
地方TableA.fKey == TableB.pKey
将导致提供程序在此处TableA.fKey == TableB.pKey
。 To select you simply need to do; 要选择,您只需要做;
select new { prop1 = TableA.Prop1, prop2 = TableB.Prop1 }
this will create a new anonymous which selects values from both tables. 这将创建一个新的匿名者,该匿名者从两个表中选择值。
Below is a more complete example of the join
syntax. 以下是
join
语法的更完整示例。 I think it uses all of the things you asked about; 我认为它使用了您询问的所有内容;
var result = from a in TableA
join b in TableB on a.fKey equals b.pKey && b.Status equals 1
select new { a.Prop1, a.Prop2, b.Prop1 };
First you need to create an auxiliar class that contains the columns of both views, something like: 首先,您需要创建一个包含两个视图的列的辅助类,如下所示:
public class viewItem
{
public int ShowID { get; set; }
public int EpisodeID { get; set; }
public int dataSource { get; set; }
...
}
then your linq query would be: 那么您的linq查询将是:
var query = (from s in db.TVData_VW_ShowList
join z in db.TVData_VW_Schedule
on s.dataSource equals z.dataSource
where s.ShowID == z.ShowID
&& s.EpisodeID == z.EpisodeID
select new viewItem {
ShowID = s.ShowID,
EpisodeID = s.EpisodeID,
dataSource = s.dataSource,
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.