通过LINQ加入列表

时间:2016-09-01 10:45:47

标签: c# linq

我有两个对象列表

任务

    public string ProjectNumber { get; set; }
    public string Name { get; set; }
    public string ProjectManager { get; set; }
    public string Customer { get; set; }

项目

    public string Number { get; set; }
    public string Title { get; set; }
    public string Manager { get; set; }
    public string Customer { get; set; }

示例数据

任务

Projectnumber | Name          | ProjectManager | Customer 
    7780      | Buy container | NULL           | NULL
    7781      | Block budget  | NULL           | NULL

项目

Nubmer        | Title         | Manager        | Customer 
    7780      | Surface       | Bill Gates     | Microsoft 
    7781      | iWatch        | Tim Cook       | Apple

我希望获得List<Task>结果:

Projectnumber | Name          | ProjectManager | Customer 
    7780      | Buy container | Bill Gates     | Microsoft 
    7781      | Block budget  | Tim Cook       | Apple

到目前为止我尝试了什么:

List<Task> spTasks = SPRepo.GetAllTasksByResourceTeam();
List<Project> projects = PWARepo.GetAllProjects();

    List<Task> joinedSPTasks = from a in spTasks
                        join b in projects on a.ProjectNumber equals b.Number
                        select new Task() {
                        a.Projectnumber = a.Projectnumber,
                        a.Name = a.Name,
                        a.ProjectManager = b.ProjectManager,
                        a.Customer = b.Manager
                        };

为什么我无法为属性赋值?我在Visual Studio中收到以下错误:

Error: Invalid anonymous type member decorator. Anonymous type members must be declared with a member assignment simple name or member access.

注意:这些对象列表来自不同的来源。无法在源系统中加入它们(例如通过表连接加入数据库)!

4 个答案:

答案 0 :(得分:2)

List<Task> joinedSPTasks = from a in spTasks
                    join b in projects on a.ProjectNumber equals b.Number
                    select new Task(){
                        ProjectNumber = a.Projectnumber,
                        Name = a.Name,
                        ProjectManager = b.ProjectManager,
                        Customer = b.Manager
                        };

var joinedSPTasks = from a in spTasks
                    join b in projects on a.ProjectNumber equals b.Number
                    select new {
                        ProjectNumber = a.Projectnumber,
                        Name = a.Name,
                        ProjectManager = b.ProjectManager,
                        Customer = b.Manager
                        };

REmove&#34; a。&#34;从匿名成员名称开始。第二个选项是使用匿名类型。您需要使用var关键字来声明匿名类型

答案 1 :(得分:2)

目前还不清楚您是想更新当前任务还是创建新任务。

创建新的将是这样的

var joinedSPTasks = from a in spTasks
                    join b in projects on a.ProjectNumber equals b.Number
                    select new Task(){
                        Projectnumber = a.Projectnumber,
                        Name = a.Name,
                        ProjectManager = b.ProjectManager,
                        Customer = b.Manager
                        };

答案 2 :(得分:0)

您正在创建的匿名类型是错误的 - 它应该是这样的:

                joinedSPTasks = from a in spTasks
                join b in projects on a.ProjectNumber equals b.Number
                select new {
                    Projectnumber = a.Projectnumber,
                    Name = a.Name,
                    ProjectManager = b.ProjectManager,
                    Customer = b.Manager
                    };

你实际上可以删除参数命名,只需这样写:

select new { a.Projectnumber, a.Name, b.ProjectManager, b.Manager };

答案 3 :(得分:0)

您正在选择动态对象中的值。而不是使用select new {,使用select new Task {

        List<Task> joinedSPTasks = (from a in spTasks
                join b in projects on a.ProjectNumber equals b.Number
                select new Task{
                    Projectnumber = a.Projectnumber,
                    Name = a.Name,
                    ProjectManager = b.ProjectManager,
                    Customer = b.Manager
                    }).ToList();