Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ为每个属性连接2个对象_C#_Linq - Fatal编程技术网

C# 使用LINQ为每个属性连接2个对象

C# 使用LINQ为每个属性连接2个对象,c#,linq,C#,Linq,我创建了两个模型来存储sql查询的结果。现在我想和他们一起参加每周的(第一周=记录id,第二周=记录id) 为了得到一个新的对象,我将拥有第一个模型中的所有数据,以及“类别”模型中的映射数据 我为它创建了一个新模型,但我不确定如何编写linq查询 第一款: public class CustomData { public string full_name { get; set; } public string location { get; set; } public i

我创建了两个模型来存储sql查询的结果。现在我想和他们一起参加每周的<代码>(第一周=记录id,第二周=记录id) 为了得到一个新的对象,我将拥有第一个模型中的所有数据,以及“类别”模型中的映射数据

我为它创建了一个新模型,但我不确定如何编写linq查询

第一款:

public class CustomData
{
    public string full_name { get; set; }
    public string location { get; set; }
    public int week1 { get; set; }
    public int week2 { get; set; }
    public int week3 { get; set; }
}
第二种模式:

public class Category
{
    public int Record_ID { get; set; }
    public int Color{ get; set; }
    public string Name { get; set; }
}
最终结果的新模型:

public class WeekView
{
    public string full_name { get; set; }
    public string location { get; set; }
    public Category week1 { get; set; }
    public Category week2 { get; set; }
    public Category week3 { get; set; }
}
这应该起作用:

        List<CustomData> list = new List<CustomData>();
        list.Add(new CustomData() { full_name = "test", location = "test", week1 = 0, week2 = 1, week3 = 2 });
        list.Add(new CustomData() { full_name = "test2", location = "test2", week1 = 0, week2 = 12, week3 = 22 });
        List<Category> categories = new List<Category>();
        categories.Add(new Category { Color = 0, Name = "testName", Record_ID = 0 });
        categories.Add(new Category { Color = 1, Name = "testName1", Record_ID = 1 });
        categories.Add(new Category { Color = 2, Name = "testName2", Record_ID = 2 });
        categories.Add(new Category { Color = 3, Name = "testName3", Record_ID = 12 });
        categories.Add(new Category { Color = 4, Name = "testName4", Record_ID = 22 });
        List<WeekView> results = new List<WeekView>();
        results.AddRange(list.Select(x=> 
              new WeekView() { full_name = x.full_name, 
                               location = x.location, 
                               week1 = categories.FirstOrDefault(c => c.Record_ID == x.week1), 
                               week2 = categories.FirstOrDefault(c => c.Record_ID == x.week2), 
                               week3 = categories.FirstOrDefault(c => c.Record_ID == x.week3)
                              }));
List List=新列表();
添加(新的CustomData(){full_name=“test”,location=“test”,week1=0,week2=1,week3=2});
添加(新的CustomData(){full_name=“test2”,location=“test2”,week1=0,week2=12,week3=22});
列表类别=新列表();
添加(新类别{Color=0,Name=“testName”,Record_ID=0});
添加(新类别{Color=1,Name=“testName1”,Record_ID=1});
添加(新类别{Color=2,Name=“testName2”,Record_ID=2});
添加(新类别{Color=3,Name=“testName3”,Record_ID=12});
添加(新类别{Color=4,Name=“testName4”,Record_ID=22});
列表结果=新列表();
results.AddRange(list.Select)(x=>
新建WeekView(){full_name=x.full_name,
位置=x.位置,
week1=categories.FirstOrDefault(c=>c.Record_ID==x.week1),
week2=categories.FirstOrDefault(c=>c.Record\u ID==x.week2),
week3=categories.FirstOrDefault(c=>c.Record\u ID==x.week3)
}));
尝试以下操作:

var result = (from cd in CustomDatas
              join ca1 in Categories on cd.week1 equals ca.Record_ID into ca1r
              from ca1 in ca1r.DefaultIfEmpty()
              join ca2 in Categories on cd.week2 equals ca.Record_ID into ca2r
              from ca2 in ca2r.DefaultIfEmpty()
              join ca3 in Categories on cd.week3 equals ca.Record_ID into ca3r
              from ca3 in ca3r.DefaultIfEmpty()
              select new {
                    full_name = cd.full_name,
                    location = cd.location,
                    week1 = ca1,
                    week2 = ca2,
                    week3 = ca3
              }

示例数据请在类别中加入ca1-类别是一种类型,在给定上下文中无效。类别在哪里是一种类型?类型为类别。在本例中,我假设有一个列表(或任何其他可枚举对象)和一组Category对象。您也可以执行
categories.FirstOrDefault(c=>c.Record\u ID==x.week1)
它将是相同的,但更简洁。