简体   繁体   English

如何从LINQ中包含的实体中进行选择,并获得类似于SQL Join的平面列表?

[英]How can I select from an included entity in LINQ and get a flat list similar to a SQL Join would give?

I have two classes: 我有两节课:

public class Topic
{
    public Topic()
    {
        this.SubTopics = new HashSet<SubTopic>();
    }
    public int TopicId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<SubTopic> SubTopics { get; set; }
}

public class SubTopic
    public int SubTopicId { get; set; }
    public int Number { get; set; }
    public int TopicId { get; set; }
    public string Name { get; set; }
    public virtual Topic Topic { get; set; }
}

What I would like to do is to get a Data Transfer Object output from LINQ that will show me. 我想做的是从LINQ获取数据传输对象输出,该输出将显示给我。 I do want to see the TopicId repeated if there is more than one SubTopic inside that topic: 如果该主题内有多个SubTopic,我确实希望重复查看TopicId:

TopicId  Name     SubTopicId  Name
1        Topic1   1           SubTopic1
1        Topic1   2           SubTopic2
1        Topic1   3           SubTopic3
2        Topic2   4           SubTopic4

I tried to code a Linq statement like this: 我试图编写这样的Linq语句:

            var r = context.Topics
                .Select ( s => new {
                        id = s.TopicId,
                        name = s.Name,
                        sid = s.SubTopics.Select( st => st.SubTopicId),
                        sidname = s.SubTopics.Select ( st => st.Name)
                    }).
            ToList();

But this does not really work as it returns sid and sidname as lists. 但这实际上并不起作用,因为它将sid和sidname返回为列表。

How will it be possible for me to get a flat output showing what I need? 我将如何获得显示我需要的固定输出?

You need SelectMany to expand a nested collection, along these lines 您需要SelectMany沿这些行扩展嵌套的集合

var r = context.Topics.SelectMany(t => t.SubTopics
 .Select(st => new 
    {
        TopicID = t.TopicId, 
        TopicName = t.Name, 
        SubTopicID = st.SubTopicId,
        SubTopicName = st.Name
    }));

try this : 尝试这个 :

var r = context.Topics
            .Select ( s => new {
                    id = s.TopicId,
                    name = s.Name,
                    sid = s.SubTopics.Where(st=>st.TopicId==s.TopicId).Select( st => st.SubTopicId ),
                    sidname = s.SubTopics..Where(st=>st.TopicId==s.TopicId).Select ( st => st.Name)
                }).
        ToList();

Hope it will help 希望对你有帮助

@Sweko provided an answer that satisfies the exact output that you requested. @Sweko提供的答案可以满足您要求的确切输出。 However, this can be even simpler if you just return the subtopic intact. 但是,如果只返回完整的子主题,这甚至会更加简单。 It may run a bit quicker as well, since you don't need to create a new object for each element in the result. 由于您不需要为结果中的每个元素创建一个新的对象,它的运行速度也可能会更快一些。

Lastly, it looks like you wanted your result set ordered. 最后,您似乎希望对结果集进行排序。 For completeness, I've added those clauses as well. 为了完整起见,我还添加了这些子句。

var r = context.Topics
           .SelectMany( topic => topic.SubTopics )
              .OrderBy(sub => sub.TopicId)
              .ThenBy(sub => sub.SubTopicId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果由包括的实体订购,如何在Linq Select Distinct中订购? - How can I order in Linq Select Distinct if ordered by an Included entity? 我如何链接两个类,使用Entity Framework和LINQ选择并仍然获得列表输出? - How can I link two classes, select and still get a list output using Entity Framework and LINQ? 如何在Entity Linq to SQL中获得唯一列表 - How to get a distinct list in Entity Linq to SQL 我如何使用 LINQ 加入、GroupBy 和 Select? - How can i Join, GroupBy and Select with LINQ? 我如何使用LINQ从父/子表中获得数据的“固定”输出 - How can I get a “flat” output of data from a parent / child table with LINQ 如何使用 Entity Framework Core 中的 Join() 方法将 Sql 查询转换为 Linq 及其等效项 - How can I convert Sql query to Linq and its equivalent with Join() method in Entity Framework Core 我怎样才能让NHibernate给我它会为插入/更新而不是执行它生成的SQL? - How can i get NHibernate to give me the SQL it would generate for an insert / update instead of executing it? 如何使用LINQ在实体框架中执行这种类型的JOIN - How can I perform this type of JOIN in Entity framework with LINQ 我怎样才能使LINQ Select包含一个子对象(如SQL外连接)? - How can I make a LINQ Select that includes a child object like a SQL outer join? 如何在linq中选择所有sql - How can I select all in linq to sql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM