Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用linq lampda表达式的sql左连接_.net_Linq - Fatal编程技术网

.net 使用linq lampda表达式的sql左连接

.net 使用linq lampda表达式的sql左连接,.net,linq,.net,Linq,我在与linq进行左/右连接时遇到问题 我不得不说 public class Customer { prop string CustomerId { get; set; } prop string LanguageGuid { get; set; } } public class ReadOnlyCustomer { prop string CustomerId { get; set; } prop string LanguageGuid { get; set; } } 我在

我在与linq进行左/右连接时遇到问题

我不得不说

public class Customer
{
  prop string CustomerId { get; set; }
  prop string LanguageGuid { get; set; }
}

public class ReadOnlyCustomer
{
  prop string CustomerId { get; set; }
  prop string LanguageGuid { get; set; }
}
我在ReadonlyCustomer表中有很多客户。 在我的情况下,我没有客户表中的所有客户。 所以我不能使用连接,我不知道什么是内部连接。 我需要左或右连接

var test = db.Customer.Join(db.ReadOnlyCustomer, p => p.CustomerId, o => o.CustomerId, (c, o) => new ReadOnlyCustomer() { CustomerId = c.CustomerId, LanguageGuid = o.LanguageGuid ?? c.LanguageGuid });
此时,我得到了一个空指针,因为查询不能连接到空引用

如何实现与sql左连接相等的左连接,其中数据源中不存在的值为NULL

这需要在lampda中,而不是像(from o in…)那样理解语法


//dennis

您需要使用
GroupJoin
,有时还需要在之后调用
DefaultIfEmpty()
为组提供一个空值。然后可以使用
SelectMany()
以每对一个结果结束,注意结果中的一个值可能为null

例如:

// Note: can only do a *left* join, so "bigger" table comes first
var test = db.ReadOnlyCustomer
             .GroupJoin(db.Customer,
                        p => p.CustomerId,
                        o => o.CustomerId,
                        (p, os) => new { p, os = os.DefaultIfEmpty() })
             // Each pair may have multiple os here
            .SelectMany(pair => pair.os.Select(o => new { pair.p, o }))
            // Flattened, but o may be null
            .Select(pair => new ReadOnlyCustomer {
                      CustomerId = pair.p.CustomerId,
                      LanguageGuid = o != null ? o.LanguageGuid ?? p.LanguageGuid
                                               : p.LanguageGuid
                    });

(出于兴趣,为什么需要使用lambda语法而不是查询表达式?通常,所有类型的联接都更容易在查询表达式中表达。)

您需要使用
GroupJoin
,有时还需要在之后调用
DefaultIfEmpty()
为组提供一个空值。然后可以使用
SelectMany()
以每对一个结果结束,注意结果中的一个值可能为null

例如:

// Note: can only do a *left* join, so "bigger" table comes first
var test = db.ReadOnlyCustomer
             .GroupJoin(db.Customer,
                        p => p.CustomerId,
                        o => o.CustomerId,
                        (p, os) => new { p, os = os.DefaultIfEmpty() })
             // Each pair may have multiple os here
            .SelectMany(pair => pair.os.Select(o => new { pair.p, o }))
            // Flattened, but o may be null
            .Select(pair => new ReadOnlyCustomer {
                      CustomerId = pair.p.CustomerId,
                      LanguageGuid = o != null ? o.LanguageGuid ?? p.LanguageGuid
                                               : p.LanguageGuid
                    });

(出于兴趣,为什么需要使用lambda语法而不是查询表达式?通常,所有类型的连接都更容易在查询表达式中表达。)

为什么不能使用“理解”语法?我所知道的唯一简单的解决方案只适用于“理解”语法:为什么要求不能使用“理解”语法?我所知道的唯一简单的解决方案只适用于“理解”语法:Thx,这很好地工作。。回答您感兴趣的问题:我需要在查询启动之前动态地向查询添加更多表达式。我只想用其中的一种方法来做,否则它就不会看起来那么好了。我确实想学习如何在这个表单中进行查询。我没有成功地在谷歌上找到解决方案,我们没有达到最佳状态:PThx,这工作很好。。回答您感兴趣的问题:我需要在查询启动之前动态地向查询添加更多表达式。我只想用其中的一种方法来做,否则它就不会看起来那么好了。我确实想学习如何在这个表单中进行查询。我没有成功地在谷歌上找到解决方案,我们没有达到最佳状态:P