LINQ to SQL 执行 Union 和 Left Outer Join
我有3张桌子。 2 包含我需要对其进行 UNION 以获得所有唯一文件的文件列表,然后我想对第三个表进行左外连接以查找仅在第三个表中而不是在另一个表中的所有文件2.
要执行 UNION,我有以下操作:
var imageUnion = (from img in dc.ImageT1
select img.filename).Union(
from img in dc.ImageT2
select img.filename);
现在,为了仅获取第三个表中的文件,我将执行左外连接,如下所示:
var query = from image in dc.ImageT1
join active in dc.ActiveImages on image.filename equals
active.filename into gj
from subimage in gj.DefaultIfEmpty()
where subimage.filename == null
select new { image.filename, image.size };
我了解如何简单地针对一个表执行左外连接,但是我该如何做将我的第一个查询的结果集获取到左外连接中? 基本上,我不想对 ImagesT1 进行左外连接,而是想对 imageUnion 结果进行左外连接。
谢谢!
I have 3 tables. 2 contain lists of files that I need to do a UNION on to get all the unique files, then I want to do a left outer join against the 3rd table to find all the files that are in the 3rd table only and not in the other 2.
To do the UNION I have the following:
var imageUnion = (from img in dc.ImageT1
select img.filename).Union(
from img in dc.ImageT2
select img.filename);
Now, to get the files only in the 3rd table I would do a left outer join as such:
var query = from image in dc.ImageT1
join active in dc.ActiveImages on image.filename equals
active.filename into gj
from subimage in gj.DefaultIfEmpty()
where subimage.filename == null
select new { image.filename, image.size };
I understand how to do the left outer join simply against ONE table, but how do I get the result set of my first query into the left outer join? Basically, instead of doing the left outer join against ImagesT1 I want to do it against the imageUnion result.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在您的联盟中选择多于一处房产; 当前结果是 IEnumerable(假设您的文件名是一个字符串)。
然后您应该能够在第二个查询中使用它来替换 dc.ImageT1。
尽管想得更多,但 Union 可能无法与 2 种匿名类型一起工作; 为了支持这一点,也许值得定义一个只有文件名和大小的类?
You need to select more than one property in your Union; The current result is IEnumerable<string> (assuming your filename is a string).
Then you should be able to use it in the second query to replace dc.ImageT1.
Though thinking more on it, the Union may not work with 2 anonymous types; To support that, maybe it'd be worth defining a class that has only a Filename and Size?
您应该能够从第一个查询中进行选择,而不是再次从图像表中进行选择。 类似:
编辑:您还必须编辑 imageUnion 查询以选择大小和文件名(以及最终查询中需要的任何其他列)。
You should be able to select from your first query instead of the image table again. Something like:
edit: You will also to have to edit your imageUnion query to select size as well as filename (and any other columns you need in your final query).