简体   繁体   English

Linq全外部联接错误

[英]Linq Full Outer Join Error

I am connected to the Adventure Works 2012 database using Linqpad as shown below: 我使用Linqpad连接到Adventure Works 2012数据库,如下所示:

在此处输入图片说明

I am trying to write a full outer join. 我正在尝试编写完整的外部联接。 My research tells me it should look like this (as shown in the screenshot): 我的研究告诉我,它应该看起来像这样(如屏幕截图所示):

var LeftOuterJoin =  from p in Products
join pv in ProductVendors on p.ProductID equals pv.ProductID into ppv
from pv in ppv.DefaultIfEmpty()
select new {ProductName = p.Name};

var RightOuterJoin =  from v in Vendors
join pv in ProductVendors on v.BusinessEntityID equals pv.BusinessEntityID into ppv
from pv in ppv.DefaultIfEmpty()
select new {VendorName = v.Name};

LeftOuterJoin.Union(RightOuterJoin);

However, the error in the screenshot (see screenshot) is: 但是,屏幕截图(请参见屏幕截图)中的错误是:

'System.Linq.IQueryable<AnonymousType#1>' does not contain a definition for 'Union' and the best extension method overload 'System.Linq.ParallelEnumerable.Union<TSource>(System.Linq.ParallelQuery<TSource>, System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments

Instance argument: cannot convert from 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.ParallelQuery<AnonymousType#2>'

What is the problem? 问题是什么?

As i can see , there is type issue , both anonymous type have different property , so that will be causing the issue for using Union . 如我所见,存在类型问题,两个匿名类型具有不同的属性,因此这将导致使用Union的问题。 you can create a common type with same property and use it. 您可以创建具有相同属性的通用类型并使用它。 or have both property and use according to query Like: 或同时具有属性和根据查询使用:

var LeftOuterJoin =  from p in Products
join pv in ProductVendors on p.ProductID equals pv.ProductID into ppv
from pv in ppv.DefaultIfEmpty()
select new {VendorName = "",
            ProductName = p.Name,};

var RightOuterJoin =  from v in Vendors
join pv in ProductVendors on v.BusinessEntityID equals pv.BusinessEntityID into ppv
from pv in ppv.DefaultIfEmpty()
select new {VendorName = v.Name,
            ProductName = "",};

var result = LeftOuterJoin.Union(RightOuterJoin).ToList(); // force execution to get results.

use this if you are using LinqPad 如果您使用的是LinqPad,请使用它

var result = LeftOuterJoin.Union(RightOuterJoin)
result .Dump();

This will work for you. 这将为您工作。

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

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