【问题标题】:Linq join 4 list <T>Linq 加入 4 列表 <T>
【发布时间】:2012-04-24 14:27:27
【问题描述】:

我有 4 个列表需要一起加入

public class Person
{
    public int personid { get; set; }
    public string fname { get; set; }
    public string lname { get; set; }
}

public class Department
{
    public int depid { get; set; }
    public string departmentname { get; set; }
}

public class Group
{
    public int groupid { get; set; }
    public int depid { get; set; }
    public string groupname { get; set; }
}

public class UploadFile
{
    public int fileid { get; set; }
    public int personid { get; set; }
    public int groupid{ get; set; }
    public string filename { get; set; }
}

List<Person> Persons=GetPersons();
List<Department> departmentlist=getDepartments();
List<Group> grouplist=getgroups();
List<UploadFile> filelist=getFiles();

我需要这样的输出:

fileid fname 文件名路径 1 个样本名 Mydoc 部门名+">"+组名

【问题讨论】:

  • 如果这些 get 函数来自同一个数据库,我建议使用 Linq2SQL 或实体框架而不是在内存中进行连接。
  • 我们不会为你做作业。向我们展示您的代码,说明您遇到的问题,然后我们可以为您提供帮助。

标签: c# linq list join


【解决方案1】:

你可以使用Zip:

List persons=GetPersons();
List departmentlist=getDepartments();
List grouplist=getgroups();
List filelist=getFiles();

var result = persons.Zip(departmentlist, (person, department) => person.fname + " " + department.departmentname)
                    .Zip(grouplist, (personAndDepartment, group) => personAndDepartment + " " + group.groupName);

// etc ...

【讨论】:

    猜你喜欢
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多