[英]How to join two tables, and retrieve item collection using Linq
I have two tables: baswareCatalog.FlowCurrent and baswareCatalog.Doc. 我有两个表:baswareCatalog.FlowCurrent和baswareCatalog.Doc。 They both contain the attribute DocId. 它们都包含属性DocId。 The Doc-table also has the attributes LastExpireDate and Status. Doc-table还具有LastExpireDate和Status属性。 FlowCurrent-table also has the attribute RecipientName. FlowCurrent-table还具有RecipientName属性。 I want to retrieve a collection of Doc, where the DocId of the item is the same in both tables. 我想检索Doc的集合,其中两个表中的项目的DocId是相同的。
Furthermore, this condition must be true: flowCurrent.RecipientName.Contains(userFullName) 此外,此条件必须为true:flowCurrent.RecipientName.Contains(userFullName)
I tried the following, but I don't know if it is correct. 我尝试了以下,但我不知道它是否正确。
var docitemIQueryable = from flowCurrent in baswareCatalog.FlowCurrent join
document in baswareCatalog.Docs on
flowCurrent.DocId equals document.DocId
where flowCurrent.RecipientName.Contains(userFullName)
select new
{
Items = baswareCatalog
.Docs
.Where(b => b.DocId == flowCurrent.DocId)
};
The return-value is 返回值是
'interface System.Linq.IQueryable<out T>'.
T is 'a'
Anonymous Types: 'a is new {IQueryable<Doc> Items }'
How do I retrieve a collection of Doc that I can iterate over? 如何检索可以迭代的Doc集合?
Without more knowledge of your table structure, you're query looks ok. 如果没有更多的表结构知识,那么查询看起来还不错。 At the point of the 'select' you have the two tables joined and limited by your where clause. 在'select'点上,你有两个表连接并受where子句的限制。
Inside the select new, you can assign to the new object whatever properties from the original tables you want to include. 在select new中,您可以为要包含的原始表中的任何属性分配新对象。
select new
{
DocId = flowCurrent.DocId,
LastExpireDate = document.LastExpireDate,
//etc
}
If you just want to get the doc items you can do this 如果您只想获取doc项,则可以执行此操作
var docitemIQueryable = from flowCurrent in baswareCatalog.FlowCurrent join
document in baswareCatalog.Docs on
flowCurrent.DocId equals document.DocId
where flowCurrent.RecipientName.Contains(userFullName)
select document;
and then you will have an iqueryable of documents 然后你将有一个可疑的文件
Two things: 两件事情:
System.Linq.IQueryable
is a collection that you can iterate over. System.Linq.IQueryable
是一个可以迭代的集合。 You can put it in a foreach
loop or do whatever you want with it. 你可以将它放在foreach
循环中,或者用它做任何你想做的事情。 Or you can use .ToList()
if you want to immediately materialize it. 或者,如果要立即实现它,可以使用.ToList()
。 FlowCurrent
and Docs
, but then you're taking the results and going right back to Docs
again. 您正在FlowCurrent
和Docs
之间进行FlowCurrent
,但之后您将获取结果并再次返回Docs
。 You don't need to do that. 你不需要这样做。 Assuming what you care about is just the stuff in Docs
, try this: 假设你关心的只是Docs
的东西,试试这个:
var docitemIQueryable = from flowCurrent in baswareCatalog.FlowCurrent
join document in baswareCatalog.Docs on
flowCurrent.DocId equals document.DocId
where flowCurrent.RecipientName.Contains(userFullName)
select document;
That will give you a collection of rows with all the fields in Docs
. 这将为您提供包含Docs
所有字段的行集合。 If you need some of the stuff in FlowCurrent
as well then you'll need to do something more like this: 如果你需要FlowCurrent
一些东西,那么你需要做更像这样的事情:
var docitemIQueryable = from flowCurrent in baswareCatalog.FlowCurrent
join document in baswareCatalog.Docs on
flowCurrent.DocId equals document.DocId
where flowCurrent.RecipientName.Contains(userFullName)
select new {
document.DocId, flowCurrent.blahblah, ...
};
The result is a sequence of objects within items property so you need a nested loop or a SelectMany 结果是items属性中的一系列对象,因此您需要嵌套循环或SelectMany
foreach(var item in docitemIQueryable.SelectMany( x=> x.Items)){
//do something
}
However it seems your inner selection returns just one element in which case you can skip the anonymously type object and simply select like below 但是,您的内部选择似乎只返回一个元素,在这种情况下,您可以跳过匿名类型对象,只需选择如下所示
select document;
and simplify the looping to 并简化循环
foreach(var item in docitemIQueryable){
//do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.