LINQ 以及如何返回特定类型的列表
我有 2 个表(Document 和 DocumentClass),它们具有以下列:
DocumentClass: DocClassID, Name, ParentID
Document: DocID, Name, DocClassID
DocumentClass
表包含父记录和子记录以及父记录之间的关系子项是 ParentID 列。 Document
记录通过 DocClassID 外键与 DocumentClass
中的子记录链接。
DocumentClass
中的父记录的 ParentID = 0,DocumentClass
中的子记录的 ParentID != 0
我想从 中检索孩子的姓名以及该孩子的父母姓名DocumentClass
表。
我已经设法创建一个为我做这件事的函数。我发送文档 ID 列表,查找文档链接到的那些 DocumentClass
记录(子记录),然后查找这些子记录的父记录。然后我将我想要的信息放入 Child 类中。
public List<Child> GetDocClassInfo(List<int> docIds)
{
var result = from dc in _context.DocClasses
from d in _context.Documents
where dc.DocClassID == d.DocClassID
where dc.DocClassID != 0
where docIds.Contains(d.DocID)
select new
{
children = from p in _context.DocClasses
where dc.ParentID == p.DocClassID
select new Child
{
ChildId = dc.DocClassID,
ChildDocClassName = dc.DocClassName,
ParentId = p.DocClassID,
ParentDocClassName = p.DocClassName
}
};
return result.ToList();
}
我的问题是我想要一个列表从函数返回,但编译器根本不喜欢这样。我收到一条错误消息
无法将类型
System.Collections.Generic.List``
隐式转换为System.Collection.Generic.List
。
如何编写 LINQ 查询来返回列表?
最好的问候,
OKB
I have 2 tables (Document and DocumentClass) that have the following columns:
DocumentClass: DocClassID, Name, ParentID
Document: DocID, Name, DocClassID
The DocumentClass
table contains parent and child records and the relationship between a parent and a child is the ParentID column. A Document
record is linked with a child record in the DocumentClass
by the DocClassID foreign key.
Parent records in DocumentClass
have ParentID = 0 and Child records in the DocumentClass
have ParentID != 0
I want to retrieve a childs Name and that child's Parents Name from the DocumentClass
table.
I Have managed to create a function that does that for me. I send in a list of Document ids, find those DocumentClass
records (the child records) that the document is linked to and then find the parent to those child records. Then I put that information I want into a Child class.
public List<Child> GetDocClassInfo(List<int> docIds)
{
var result = from dc in _context.DocClasses
from d in _context.Documents
where dc.DocClassID == d.DocClassID
where dc.DocClassID != 0
where docIds.Contains(d.DocID)
select new
{
children = from p in _context.DocClasses
where dc.ParentID == p.DocClassID
select new Child
{
ChildId = dc.DocClassID,
ChildDocClassName = dc.DocClassName,
ParentId = p.DocClassID,
ParentDocClassName = p.DocClassName
}
};
return result.ToList();
}
My problem is that I want to have a List to return from the function, but the compiler doesn't like this at all. I get an error saying that
Cannot implicitly convert type
System.Collections.Generic.List``<AnonymousType#1>
toSystem.Collection.Generic.List<Child>
.
How can I write that LINQ query to return a List?
Best regards,
OKB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
result
是一个匿名类型列表,每个匿名类型都有一个成员 (children
),该成员是一组可枚举的Child
记录。您应该能够在此处使用 SelectMany:或(相同):
result
is a list of anonymous types, each with a member (children
) that is an enumerable set ofChild
records. You should be able to use SelectMany here:or (identical):
当您说
select new {children ...
时,您将返回匿名类型You are returning an anonymous type when you say
select new { children ...