简体   繁体   English

LINQ Enumerable中的Enumerable-为什么?

[英]LINQ Enumerable in Enumerable - Why?

I have a Ticket class that contains a List of User objects: 我有一个Ticket类,其中包含一个User对象列表:

class Ticket {
    public List<User> Users { get; set; }
}

I want to get all Users that are related to tickets. 我想获取所有与票证相关的用户。 I do this: 我这样做:

var u = from t in db.Tickets
        where t.Users.Count() > 0
        select t.Users

That works but returns a Enumerable with 1 element. 可以,但是返回一个带有1元素的Enumerable And that element is an Enumerable with the Users. 该元素是用户可Enumerable的。

Why is it nested? 为什么嵌套?

What you need to do is 您需要做的是

  var u = (from t in db.Tickets from user in t.Users select user).ToList();

Then you will have a flattened List of Users. 然后,您将获得一个扁平的用户列表。 There's no need to check for where t.Users.Count() > 0 and if you did need to you should be using where t.Users.Any() for efficiency. 无需检查where t.Users.Count() > 0 ,如果确实需要,则应使用t.Users.Any()的效率。

Because you select a IEnumerable<Users> of List<User> ... 因为您选择了List<User>IEnumerable<Users> List<User> ...

The second Enumerable is your List<User> 第二个Enumerable是您的List<User>

You can flatten that with the following query. 您可以使用以下查询将其展平。

db.Tickets.Where(t => t.Users.Any()).SelectMany(t => t.Users)

Any() is much faster than Count() > 0 because it will stop couting at the first occurence, Count iterate on the full list of users you have Any()Count() > 0快得多,因为它会在第一次出现时停止计算,对您拥有的完整用户列表进行Count迭代

Because it doesn't merge in one list all t.Users (t0.Users, t1.Users...). 因为它不会合并在所有t.Users中(t0.Users,t1.Users ...)。 You need to combine them. 您需要将它们结合起来。

您可以将其合并为IEnumerable,如下所示:db.Tickets.Where(t => t.Users.Count()> 0).SelectMany(t => t.Users)

I think you are looking for 我想你在找

var u = (from t in db.Tickets
         from u in t.Users
         select u).Distinct();

From the query i supose you want to have all the user in the tickets. 从查询我想你想让所有用户都在票证中。

You can solve this like this: 您可以这样解决:

List<User> users = new List<User>();

foreach(List<user> u in  db.Tickets.where(ticket => ticktet.Users.Count() > 0).select(ticket => ticket.Users)){
users.AddAll(u);
}            

The SelectMany method is what you want. SelectMany方法就是您想要的。 From the MSDN documentation: 从MSDN文档中:

"Projects each element of a sequence to an IEnumerable(Of T) and flattens the resulting sequences into one sequence." “将序列的每个元素投影到IEnumerable(T)并将结果序列展平为一个序列。”

So you take all the tickets and get all the users for each ticket so the resulting list will be a list of users. 因此,您获取了所有票证并获得了每个票证的所有用户,因此结果列表将是用户列表。 You also might want to use the Distinct method as well to remove duplicate users. 您可能还需要使用Distinct方法来删除重复的用户。 I'm assuming that a user might be associated with more than one ticket. 我假设一个用户可能与多个票证相关联。

First: A LINQ-Query will always return an enumerable. 第一:LINQ查询将始终返回可枚举。

Second: Your t.Users is a List, which implements IEnumerable aswell. 第二:您的t.Users是一个List,它也实现IEnumerable。

So the outer enumerable is the linq resultset and the inner one the actual first result that can be cast back to a list of users like 因此,外部可枚举是linq结果集,内部可枚举是实际的第一个结果,可以将其返回给用户列表,例如

var u = from t in db.Tickets
       where t.Users.Count() > 0
       select (t.Users as List<User)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM