Asp.net mvc 带where条件的LINQ连接
我可以使用LINQ的连接和Lambda符号,这没问题,但我无法计算如何添加where条件Asp.net mvc 带where条件的LINQ连接,asp.net-mvc,linq,entity-framework,lambda,Asp.net Mvc,Linq,Entity Framework,Lambda,我可以使用LINQ的连接和Lambda符号,这没问题,但我无法计算如何添加where条件 var q = query.Join(context.CustomerIds, x => x.CustomerId, y => y.CustomerId, (x, y) => new CustomerLookupResult() { dob = x.DateOfBirth.ToString(), forenames = x.F
var q = query.Join(context.CustomerIds,
x => x.CustomerId,
y => y.CustomerId,
(x, y) => new CustomerLookupResult()
{
dob = x.DateOfBirth.ToString(),
forenames = x.Forenames,
surname = x.Surname,
loyaltyNo = y.Identifier,
customerId = x.CustomerId
});
我要加入的第一个表在其标识符列中包含忠诚号,但在同一列中也包含其他信息,因此使用第二列IdentifierTypeCode进行过滤
那么我现在如何像在SQL中一样添加.Wherex=>x.IdentifierTypeCode==忠诚度呢。将此附加到末尾将引用新对象。您可以在执行连接之前应用Where
var q = customerLoyalties
.Where(x => x.IdentifierTypeCode == "LOYALTY")
.Join(customers,
x => x.CustomerId,
y => y.CustomerId,
(x, y) => new CustomerLookupResult()
{
CustomerId = y.CustomerId,
Name = y.Name,
IdentifierTypeCode = x.IdentifierTypeCode
});
在加入之前,您可以在何处应用
var q = customerLoyalties
.Where(x => x.IdentifierTypeCode == "LOYALTY")
.Join(customers,
x => x.CustomerId,
y => y.CustomerId,
(x, y) => new CustomerLookupResult()
{
CustomerId = y.CustomerId,
Name = y.Name,
IdentifierTypeCode = x.IdentifierTypeCode
});
您也可以使用这种方法来实现使用Linq
var match = from t1 in context.orders
join t2 in context.orderdetails on
new { t1.OrderID } equals
new { t2.OrderID }
join t3 in context.products on
new { t2.ProductID } equals
new { t3.ProductID }
where t3.ProductID == id
select t3;
return match.ToList();
您也可以使用这种方法来实现使用Linq
var match = from t1 in context.orders
join t2 in context.orderdetails on
new { t1.OrderID } equals
new { t2.OrderID }
join t3 in context.products on
new { t2.ProductID } equals
new { t3.ProductID }
where t3.ProductID == id
select t3;
return match.ToList();
连接的第一个参数采用任何IEnumerable,因此可以在该点或更早的时间应用Where
var q = query.Join(context.CustomerIds.Where(x=>x.IdentifierTypeCode=="LOYALTY"),
x => x.CustomerId,
y => y.CustomerId,
(x, y) => new CustomerLookupResult()
{
dob = x.DateOfBirth.ToString(),
forenames = x.Forenames,
surname = x.Surname,
loyaltyNo = y.Identifier,
customerId = x.CustomerId
});
或者,如果您不想在一行上放太多内容:
var filteredLoyalties = context.CustomerIds.Where(x=>x.IdentifierTypeCode=="LOYALTY");
var q = query.Join(filteredLoyalties,
...
连接的第一个参数采用任何IEnumerable,因此可以在该点或更早的时间应用Where
var q = query.Join(context.CustomerIds.Where(x=>x.IdentifierTypeCode=="LOYALTY"),
x => x.CustomerId,
y => y.CustomerId,
(x, y) => new CustomerLookupResult()
{
dob = x.DateOfBirth.ToString(),
forenames = x.Forenames,
surname = x.Surname,
loyaltyNo = y.Identifier,
customerId = x.CustomerId
});
或者,如果您不想在一行上放太多内容:
var filteredLoyalties = context.CustomerIds.Where(x=>x.IdentifierTypeCode=="LOYALTY");
var q = query.Join(filteredLoyalties,
...
不知道这两个列表的内容,比如:var q=query.Joincontext.CustomerIds.Wherex=>x.IdentifierTypeCode==忠诚度,当然,就是这样!非常感谢。把它作为答案,我会接受它。我认为在不知道两个列表的内容的情况下,在lamdas上使用查询语法编写LINQ更容易理解,比如:var q=Query.Joincontext.CustomerIds.Wherex=>x.IdentifierTypeCode==忠诚度,当然,就是这样!非常感谢。把它作为答案,我会接受的。我认为加入LINQ比使用lamdas上的查询语法更容易理解