一个改变LINQ的函数

时间:2013-09-11 07:49:59

标签: c# linq

我有一个嵌套列表

List<Employee> emplist = new List<Employee>()
{
    new Employee{Age=15, name = "Tom", Projects = new List<project>
    {
        new project { ID = 12, code = 22 ,name = "Project A"},
        new project { ID = 11, code = 23 ,name = "Project B"},
        new project { ID = 16, code = 25 ,name = "Project C"}
    }},
    new Employee{Age=17, name = "Billy", Projects = new List<project>
    {
        new project { ID = 17, code = 20 ,name = "Project D"},
        new project { ID = 18, code = 12 ,name = "Project E"},
        new project { ID = 10, code = 9 ,name = "Project F"}
    }},
    new Employee{Age=25, name = "Sam", Projects = new List<project>
    {
        new project { ID = 22, code = 2 ,name = "Project X"},
        new project { ID = 24, code = 5 ,name = "Project Y"},
        new project { ID = 19, code = 1 ,name = "Project Z"}
    }}
};

这是一个LINQ来搜索ID(= 24)的最大值并获得索引(= 2)和子索引(= 1)

var result =  emplist.Select((x,i) => new { index = i, item = x})
                 .SelectMany(x => x.item.project.Select(
                      (a,i) => new { index = x.index, subindex = i, id = a.ID}))
                 .OrderByDescending(x => x.id )
                 .First();

我需要为不同的变量多次执行这个特定的LINQ(比如说我想找到最大代码而不是ID)。有可能有这样的功能所以我可以快速改变LINQ吗?下面的功能是无稽之谈,我只想说明这个想法

public void QuickLINQ(string Parameter1, string Parameter2)
{
var result =  emplist.Select((x,i) => new { index = i, item = x})
                 .SelectMany(x => x.item.Parameter1.Select(
                      (a,i) => new { index = x.index, subindex = i, id = a.Parameter2}))
                 .OrderByDescending(x => x.id )
                 .First();
}

3 个答案:

答案 0 :(得分:2)

我知道这看起来并不像LINQ表达式那样性感,但效率更高。

此版本将允许您指定用于选择适当项目的标准:

// selector is a function that compares two projects and returns true if we should keep the new one
static void GetIndexes(List<Employee> emplist, out int index, out int subindex, Func<project, project, bool> selector) {
    index = -1;
    subindex = -1;
    project selected = null;
    for (int i = 0; i < emplist.Count; i++) {
        var emp = emplist[i];
        for (int j = 0; j < emp.Projects.Count; j++) {
            var prj = emp.Projects[j];
            if (selected == null || selector(selected, prj)) {
                selected = prj;
                index = i;
                subindex = j;
            }
        }
    }
}

然后你这样称呼它:

int index = 0;
int subindex = 0;

// select by max ID
GetIndexes(emplist, out index, out subindex, (oldPrj, newPrj) => newPrj.ID > oldPrj.ID );

// select by minimum Code
GetIndexes(emplist, out index, out subindex, (oldPrj, newPrj) => newPrj.Code < oldPrj.Code);

注意:我修改了代码使其成为一个函数,希望我明白你的意思......

答案 1 :(得分:0)

这个怎么样:

var maxEmployee=emplist.OrderByDecending(p=>p.projects.Max(k=>k.ID))
               .FirstOrDefault();

maxEmployee将拥有最高的项目ID。

答案 2 :(得分:0)

我无法完全理解你的问题。但我想你问过这种类型的东西.....
如果你有任何问题评论我...因为我没有执行它。只是得到一个想法.......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace example
{
[Serializable]
class Project
{
    public int ID { get; set; }
    public string name { get; set; }
}

[Serializable]
class Employee
{
    public int Age{get;set;}
    public string name{get;set;}
    public List<Project> projects { get; set; }
}

public class mainClass
{
    List<Employee> emplist = new List<Employee>();
    List<Project> prjlist = new List<Project>();

    public mainClass()
    {

        prjlist.Add(new Project{ID = 12, name = "Project A"});
        prjlist.Add(new Project{ID = 11, name = "Project B"});
        prjlist.Add(new Project{ID = 16, name = "Project C"});
        emplist.Add(new Employee{Age=15, name = "Tom",projects=prjlist});

        prjlist=null;

        prjlist.Add(new Project{ID = 17, name = "Project D"});
        prjlist.Add(new Project{ID = 18, name = "Project E"});
        prjlist.Add(new Project{ID = 10, name = "Project F"});
        emplist.Add(new Employee{Age=17, name = "Billy",projects=prjlist});

        prjlist=null;

        prjlist.Add(new Project{ID = 22, name = "Project X"});
        prjlist.Add(new Project{ID = 24, name = "Project Y"});
        prjlist.Add(new Project{ID = 19, name = "Project Z"});
        emplist.Add(new Employee{Age=25, name = "Sam",projects=prjlist});
    }

    public void showme()
    {
        int i=24;
        var result = from emp in emplist
                     select (emp.projects as List<Project>).Where(obj => obj.ID == i);
    }
}
}