[英]Linq returned results as condition to another linq query
var query_1 = from m in db.tableA
join n in db.tableB
on m.OwnerID equals n.OwnerID into tabA
from a in tabA
join o in db.tableC
on m.OwnerID equals o.OwnerID
where m.UserID == userSessionID
&& m.ActiveStatus == 1
select new { OwnerName = m.OwnerName };
var query_2 = from m in db.tableD
join n in db.tableE
on m.OwnerID equals n.OwnerID into tabX
from a in tabX
join o in db.tableF
on m.OwnerID equals o.OwnerID
where {?????????????}
select new { ...... };
From the above code, query_1 will return me a list of elements retrieve from database. 从上面的代码中,query_1将向我返回从数据库检索的元素列表。 What I need is I only want the first item in the query_1 and using this element's column as a condition in my query_2.
我需要的是我只希望query_1中的第一项,并将此元素的列用作我的query_2中的条件。 All this while i was using foreach to loop thru query_1 to get all the element but this time i only want the first item.
当我使用foreach来遍历query_1以获取所有元素时,所有这一切,但是这次我只想要第一项。 Note that the list of query_1 may return me null and hence hardcode is not allow.
请注意,query_1的列表可能会返回null,因此不允许使用硬编码。 Any luck?
运气好的话?
after query_1; 在query_1之后;
var firstOwner = query_1.FirstOrDefault() ?? string.Empty;
then in query_2, assuming m ( db.tableD
) has an OwnerName
property... 然后在query_2中,假设m(
db.tableD
)具有OwnerName
属性...
where m.OwnerName = firstOwner;
Version 2 , If you need an OwnerId
from query_1 : 第2版 ,如果您需要来自query_1的
OwnerId
:
change it to 更改为
select new { OwnerName = m.OwnerName, OwnerId = m.OwnerId };
than 比
var firstOwner = query_1.FirstOrDefault();
var firstOwnerId = firstOwner == null ? -1 : firstOwner.OwnerId;
and in query_2 并在query_2中
where m.OwnerId = firstOwnerId;
Try putting it on a list then get the top most record as .First()? 尝试将其放在列表中,然后以.First()的形式获得最高记录?
List<object> first = new List<object>();
first = query_1.toList();
then on your query_2 put this: 然后在您的query_2上输入以下内容:
where m.OwnerId = first.First();
EDITED: 编辑:
Or simply put this on your query_2: 或者只是将其放在您的query_2上:
where m.OwnerId = query_1.First();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.