LINQ to SQL 执行 Union 和 Left Outer Join

发布于 2024-07-16 05:44:48 字数 793 浏览 8 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-07-23 05:44:48

您需要在您的联盟中选择多于一处房产; 当前结果是 IEnumerable(假设您的文件名是一个字符串)。

var imageUnion = (from img in dc.ImageT1
                  select new { Filename = img.filename, Size = img.size }).Union(
                  from img in dc.ImageT2
                  select new { Filename = img.filename, Size = img.size });

然后您应该能够在第二个查询中使用它来替换 dc.ImageT1。

尽管想得更多,但 Union 可能无法与 2 种匿名类型一起工作; 为了支持这一点,也许值得定义一个只有文件名和大小的类?

public class TempImage
{
    public string Filename { get; set; }
    public int Size { get; set; }
}

var imageUnion = (from img in dc.ImageT1
                  select new TempImage() { Filename = img.filename, Size = img.size }).Union(
                  from img in dc.ImageT2
                  select new TempImage() { Filename = img.filename, Size = img.size });

You need to select more than one property in your Union; The current result is IEnumerable<string> (assuming your filename is a string).

var imageUnion = (from img in dc.ImageT1
                  select new { Filename = img.filename, Size = img.size }).Union(
                  from img in dc.ImageT2
                  select new { Filename = img.filename, Size = img.size });

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?

public class TempImage
{
    public string Filename { get; set; }
    public int Size { get; set; }
}

var imageUnion = (from img in dc.ImageT1
                  select new TempImage() { Filename = img.filename, Size = img.size }).Union(
                  from img in dc.ImageT2
                  select new TempImage() { Filename = img.filename, Size = img.size });
陌伤ぢ 2024-07-23 05:44:48

您应该能够从第一个查询中进行选择,而不是再次从图像表中进行选择。 类似:

var query = from image in imageUnion
            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 };

编辑:您还必须编辑 imageUnion 查询以选择大小和文件名(以及最终查询中需要的任何其他列)。

You should be able to select from your first query instead of the image table again. Something like:

var query = from image in imageUnion
            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 };

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文