我从数据库中获取一些数据,并使用 linq 计算总和和计数并对数据进行分组。
这就是我所拥有的:
var si = _repository.GetAllByDate(date);
var cs = from s in si
group s by s.Name into g
select new { Comm = g.Key, SIList = g.ToList(), Count = g.Count() };
我现在需要将 cs 传递给另一个类中的方法,以便我可以为组中的每个项目提取 Comm、SIList 和 Count,我应该将其传递为什么类型? IEnumerable 不起作用。实际的 linq 组结果类型似乎是:
{System.Linq.Enumerable.WhereSelectEnumerableIteratorf__AnonymousTyped,int>>}
有什么想法吗?我实际上想将 cs 作为变量传递并在那里迭代它。
I'm getting some data from my database and using linq to calculate sums and counts and group the data.
This is what I have:
var si = _repository.GetAllByDate(date);
var cs = from s in si
group s by s.Name into g
select new { Comm = g.Key, SIList = g.ToList(), Count = g.Count() };
i now need to pass cs to a method in another class so that I can extract Comm, SIList and Count for each item in the group, what type do I pass it as? IEnumerable doesn't work. The actual linq group result type seems to be:
{System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Linq.IGrouping<Model.Domain.MasterData
.MyItem,Model.Domain.SI<>f__AnonymousTyped<Model.Domain.MasterData.MyItem,System.Collections.Generic.List<Model.Domain.SI>,int>>}
Any ideas? I effectively want to pass cs as a variable and iterate through it there.
发布评论
评论(5)
如果要在不同的范围中使用,您需要创建一个与匿名类型的定义相匹配的类型。
编辑:我想我们可以假设该列表将是
String
,所以我正在对此进行编辑。如果这是错误的类型,您需要相应地更改IList
定义。You'll need to create a type that matches the definition of your anonymous type, if it's going to be used in different scopes.
EDIT: I supposed we can assume that the list will be of
String
so I'm editing for that. If that's the wrong type you'll need to change theIList<T>
definition accordingly.得到如此复杂的类型的原因是因为查询使用延迟执行。您正在查看返回结果的表达式的类型,而不是结果的类型。
结果的类型为 IEnumerable<_hidden_internal_class_name_>,即当您在查询中创建匿名对象时,结果是编译器内部创建的类的对象流。
将该结果传递给另一个方法是毫无用处的,因为它需要使用反射来读取对象中的属性。您应该为结果中的对象创建一个命名类,以便可以轻松访问其属性。
The reason that you get such a complicated type is because the query uses lazy execution. You are looking at the type of the expression that returns the result, not the type of the result.
The type of the result is
IEnumerable<_hidden_internal_class_name_>
, i.e. as you are creating anonymous objects in the query, the result is a stream of objects of a class that the compiler creates internally.It's pretty useless to pass on that result to another method, as it would need to use reflection to read the properties in the objects. You should create a named class for the objects in the result, so that it's easy to access its properties.
创建类型是一个很好的主意,但是当返回 Tuple< 时为什么要这样做/a> 可以在不创建新类或结构的情况下完成吗?如果需要是本地和/或内部的并且该类不会被重用,请尝试使用元组。
Creating a type is an excellent idea, but why do that when a returned Tuple can be done without creating a new class or struct? If the need is local and or internal and the class won't be reused, try using a Tuple instead.
将其作为
object
传递,并在foreach
循环中使用var
作为迭代器。Pass it as
object
and in yourforeach
loop, usevar
as the iterator.