我试图通过Linq理解lambda表达式,但我为转换而苦恼。下面是可以正常工作的Linq Query语法。
var systemUsersPoor =
(from customer in customers
join distributor in distributors
on new { customer.Location }
equals new{ distributor.Location }
where customer.Location == "UK" &&
customer.Location==distributor.Location &&
customer.Supplier == "MoneyShits" &&
distributor.Products == "ShittyCrappyCraps"
orderby customer.Name
select new
{
customerName=customer.Name , customerLocation=customer.Location, customerSupplier=customer.Supplier,
distributorName=distributor.Name, distributorProducts=distributor.Products
}).ToList();
然后在这里,我尝试将其转换为Linq方法语法失败。...直到.Where语句..表明它对我的字段(.location,.Supplier)没有定义时,所有方法都有效。和分销商
var sysUserPoor2 = customers.Join //from customer in customers Join
(
distributors, //Join on distributors on customer.Location==distribution.Location
customer=> customer.Location, //Select the primary key (the first part of the "on" clause in an sql "join" statement
distributor =>distributor.Location, // Select the foreign key (the second part of the "on" clause)
(customer, distributor) => new //select statement
{
customerName = customer.Name,
customerLocation = customer.Location,
customerSupplier = customer.Supplier,
distributorName = distributor.Name,
distributorProducts = distributor.Products
}
)
.Where
(
customer => (customer.customerLocation == "UK") &&
(customer.customerSupplier == "MoneyShits"),
distributor => distributor.distributorProducts == "ShittyCrappyCraps",
(customer, distributor) => (customer.customerLocation == distributor.Location)
);
使用以下代码的查询有效,但我不知道如何以其他方式添加其余部分...:
.Where
(
customer => (customer.customerLocation == "UK") &&
(customer.customerSupplier == "MoneyShits")
)
答案 0 :(得分:0)
请尝试以下。
var systemUsersPoor = customers.Join(distributors,
customer => customer.Location,
distributor => distributor.Location,
(customer, distributor) => new {customer, distributor})
.Where(x => x.customer.Location.Equals("UK") &&
x.customer.Location.Equals(x.distributor.Location) &&
x.customer.Supplier.Equals("MoneyShits") &&
x.distributor.Products.Equals("ShittyCrappyCraps"))
.OrderBy(x => x.customer.Name)
.Select(x => new
{
customerName = x.customer.Name,
customerLocation = x.customer.Location,
customerSupplier = x.customer.Supplier,
distributorName = x.distributor.Name,
distributorProducts = x.distributor.Products
}).ToList();