如何在第二次查询中替换ProductID的值?
var pid= from t in Products where t.Name == "Cable Lock" select t.ProductID
var f=from k in SalesOrderDetails where k.ProductID= select k;
f.Dump();
答案 0 :(得分:1)
尝试
var orderDetails = from o in SalesOrderDetails
join p in Products on p.ProductID equals o.ProductID
where (p.Name == "Cable Lock")
select o;
或使用子查询:
var orderDetails = from o in SalesOrderDetails
where Products.Any(p => p.Name == "Cable Lock")
select o;
答案 1 :(得分:0)
我不确定你为什么不去Joins
,但如果我按照你的逻辑,你在代码中遗漏FirstOrDefault
,你可以这样做: -
var pid = (from t in products where t.Name == "Cable Lock" select t.ProductID).FirstOrDefault();
然后,您可以在下一个查询中使用此productId
: -
var f= from k in SalesOrderDetails where k.ProductId == pid select k;
答案 2 :(得分:0)
您遇到的问题是var pid= from t in Products where t.Name == "Cable Lock" select t.ProductID
类型为IQueryable<int>
,IEnumerable
不是单一值。
所以你可以选择以下其中一个
var pid = (from p in Products
where p.Name == "Cable Lock"
select p.Id).Single();
var f = from k in SalesOrderDetails where k.ProductID= select k;
OR
var pid = from p in Products
where p.Name == "Cable Lock"
select p.Id;
var f = from k in SalesOrderDetails where pid.Contains(k.ProductID) select k;