LINQ 以及如何返回特定类型的列表

发布于 2024-09-14 04:12:58 字数 1756 浏览 4 评论 0原文

我有 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> to System.Collection.Generic.List<Child>.

How can I write that LINQ query to return a List?

Best regards,

OKB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲鸠 2024-09-21 04:12:58

result 是一个匿名类型列表,每个匿名类型都有一个成员 (children),该成员是一组可枚举的 Child 记录。您应该能够在此处使用 SelectMany:

var list = (from item in result
            from child in item.children
            select child).ToList();

或(相同):

var list = result.SelectMany(item => item.children).ToList();

result is a list of anonymous types, each with a member (children) that is an enumerable set of Child records. You should be able to use SelectMany here:

var list = (from item in result
            from child in item.children
            select child).ToList();

or (identical):

var list = result.SelectMany(item => item.children).ToList();
末骤雨初歇 2024-09-21 04:12:58

当您说select new {children ...时,您将返回匿名类型

You are returning an anonymous type when you say select new { children ...

猫七 2024-09-21 04:12:58
var result = (from dc in _context.DocClasses
             join d in _context.Documents
             on dc.DocClassID equals d.DocClassID
             where dc.DocClassID != 0 && docIds.Contains(d.DocID)
             let 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
                                              }
              select children).SelectMany(c=>c).ToList();
var result = (from dc in _context.DocClasses
             join d in _context.Documents
             on dc.DocClassID equals d.DocClassID
             where dc.DocClassID != 0 && docIds.Contains(d.DocID)
             let 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
                                              }
              select children).SelectMany(c=>c).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文