如何将LINQ结果传递给方法? [英] How to pass a LINQ result to a method?

查看:118
本文介绍了如何将LINQ结果传递给方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

高效的数据结构来保持员工的活动?,我有一个ActividadEmpleado类型的列表被声明为:

From Efficient data structure to hold employee's activities? , i have a List of type ActividadEmpleado which is declared as:

public string Empleado { get; set; }
public DateTime Fecha { get; set; }
public string Actividad { get; set; }

LINQ查询变量以我需要的方式重新排序结果,这是按日期和然后通过ActividadEmpleado和一个字符串。然而,var类型不能传递给方法,所以搜索这个站点我发现我需要创建一个类来存储结果或修改LINQ变量返回一个列表,但是我有正确的声明的问题。

The LINQ query variable reorders the result in the way i need, which is to store by date and then by ActividadEmpleado and a string. However, var types cannot be passed to methods, so searching this site i am finding out that i either need to create a class to store the results or to modify the LINQ variable to return a List, but i am having issues with the proper declaration.

LINQ变量是:

var queryActividades = listaActividad
                .GroupBy(a => a.Fecha, (fecha, fechaActividades) => new
                {
                    Fecha = fecha,
                    FechaActividades = fechaActividades
                        .GroupBy(a => a.Empleado, (nombreEmpleado, actividadesEmpleado) => new
                        {
                            Actividades = actividadesEmpleado,
                            NombreEmpleado = nombreEmpleado
                        })
                        .OrderBy(a => a.NombreEmpleado)
                })
                .OrderBy(a => a.Fecha);

Visual Studio表示queryActividades是:

Visual Studio says that queryActividades is:

IOrderedEnumerable<'a>
Anonymous Types:
'a new datetime fecha, iorderedenumerable<'b>
'b new IEnumerable<ActividadEmpleado> Actividades, string NombreEmpleado

我需要将queryActividades传递给另一种方法。我尝试传递它作为一个对象,但是我失去了扩展方法,例如Where(我可以以某种方式转发它)

I need to pass queryActividades to another method. I tried passing it as an Object but then i lose the extension methods such as Where<> (can i cast it somehow?)

我也读取声明结果为一个元组应该工作,但我认为宣布一个新课程是更干净的。

I also read that declaring the results as a tuple should work, but i think declaring a new class is cleaner.

我刚刚开始使用LINQ,我已经避免使用常规的数据结构,但在这种情况下,这真的很有帮助,想知道如何处理匿名类型他们或将结果转换为常规列表

I am just starting with LINQ, i have avoided it to use regular data structures but in this case it's really helpful and would like to know how to either handle anonymous types in them or convert the result to a regular List

最终解决方案:

class GrupoActividad
{
    public DateTime Fecha { get; set; }
    public IEnumerable<Actividad> Actividades { get; set; }
}

class Actividad
{
    public IEnumerable<ActividadEmpleado> Actividades { get; set; }
    public string NombreEmpleado { get; set; }
}

var queryActividades = listaActividad
                .GroupBy(a => a.Fecha, (fecha, fechaActividades) => new GrupoActividad
                {
                    Fecha = fecha,
                    Actividades = fechaActividades
                        .GroupBy(a => a.Empleado, (nombreEmpleado, actividadesEmpleado) => new Actividad
                        {
                            Actividades = actividadesEmpleado,
                            NombreEmpleado = nombreEmpleado
                        })
                        .OrderBy(a => a.NombreEmpleado)
                })
                .OrderBy(a => a.Fecha);

接收方式:

var actividades = from a in queryActividades
                       where a.Fecha == fechaCiclo
                       select new { a.Fecha, a.Actividades };
            foreach (var item in actividades)
            {
                //cycle that holds each day's activities
                foreach (var empleado in item.Actividades)
                {
                    //cycle that holds each employee with activities in that day
                    foreach (var actividad in empleado.Actividades)
                    {
                        //final cycle that actually reads the activity
                        ActividadEmpleado actividadEmpleado = (ActividadEmpleado)actividad;
                    }
                }
            }


推荐答案

现在,您正在创建一个基于匿名类型(实际上是两个匿名类型)的集合,实际上不能传递给另一个方法(除了使用反射或动态)。最简单的方法是创建一个表示集合的具体类型,如

Right now you are creating a collection that's based on an anonymous type (actually two anonymous types), which cannot practically be passed to another method (other than by using reflection or dynamic). The cleanest way is to create a concrete type that represents the collection - something like

public class ActivityGroup
{
    public DateTime Fecha {get; set;}
    public IEnumerable<Activity> Activities {get; set;}
}

public class Activity
{
    public IEnumerable<Activity> Actividades {get; set;}
    public string NombreEmpleado {get; set;}
}

然后将您的查询更改为:

then change your query to:

var queryActividades = listaActividad
                .GroupBy(a => a.Fecha, (fecha, fechaActividades) => new ActivityGroup
                {
                    Fecha = fecha,
                    FechaActividades = fechaActividades
                        .GroupBy(a => a.Empleado, (nombreEmpleado, actividadesEmpleado) => new Activity
                        {
                            Actividades = actividadesEmpleado,
                            NombreEmpleado = nombreEmpleado
                        })
                        .OrderBy(a => a.NombreEmpleado)
                })
                .OrderBy(a => a.Fecha);

并将其作为将其传递给IEnumerable< ActivityGroup>

这篇关于如何将LINQ结果传递给方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆