C# 在LINQ中加入并分组
以下是我的输入表:C# 在LINQ中加入并分组,c#,linq,join,group-by,C#,Linq,Join,Group By,以下是我的输入表: Persons Table: ================ ID Code ------------------------ 1 Person1 # Person1: John Smith, 25, 50Kg 2 Person2 # Person2: William Brown, 30, 80Kg 3 Person3 # Person3: James Miller, 32, 73Kg StringProperties Table:
Persons Table:
================
ID Code
------------------------
1 Person1 # Person1: John Smith, 25, 50Kg
2 Person2 # Person2: William Brown, 30, 80Kg
3 Person3 # Person3: James Miller, 32, 73Kg
StringProperties Table:
=========================
ID PersonID Name Value
----------------------------------------------
1 1 FirstName John # Person1: John Smith, 25, 50Kg
2 1 LastName Smith # Person1: John Smith, 25, 50Kg
3 2 FirstName William # Person2: William Brown, 30, 80Kg
4 2 LastName Brown # Person2: William Brown, 30, 80Kg
5 3 FirstName James # Person3: James Miller, 32, 73Kg
6 3 LastName Miller # Person3: James Miller, 32, 73Kg
NumericProperties Table:
=========================
ID PersonID Name Value
-----------------------------------------
1 1 Age 25 # Person1: John Smith, 25, 50Kg
2 1 Weight 50 # Person1: John Smith, 25, 50Kg
3 2 Age 30 # Person2: William Brown, 30, 80Kg
4 2 Weight 80 # Person2: William Brown, 30, 80Kg
5 3 Age 32 # Person3: James Miller, 32, 73Kg
6 3 Weight 73 # Person3: James Miller, 32, 73Kg
我想编写一个LINQ查询,生成以下结果:
Result:
==========
Code FirstName LastName Age Weight
-----------------------------------------------------------------
Person1 John Smith 25 50
Person2 William Brown 30 80
Person3 James Miller 32 73
这是我的代码,但无法正常工作:
var q = from p in db.Persons
join s in db.StringProperties on p.ID equals s.PersonID
join n in db.NumericProperties on p.ID equals n.PersonID
group p by p.Code into g
select new
{
g.Key,
g
};
假设我们有这样的实体:
public class Person
{
public int ID { get; set; }
public string Code { get; set; }
}
public class StringProperty
{
public int ID { get; set; }
public int PersonID { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class NumericProperty
{
public int ID { get; set; }
public int PersonID { get; set; }
public string Name { get; set; }
public int Value { get; set; }
}
var persons = new List<Person>
{
new Person { ID = 1, Code = "Person1" },
new Person { ID = 2, Code = "Person2" },
new Person { ID = 3, Code = "Person3" }
};
var stringProperties = new List<StringProperty>
{
new StringProperty { ID = 1, PersonID = 1, Name = "FirstName", Value = "John" },
new StringProperty { ID = 2, PersonID = 1, Name = "LastName", Value = "Smith" },
new StringProperty { ID = 3, PersonID = 2, Name = "FirstName", Value = "William" },
new StringProperty { ID = 4, PersonID = 2, Name = "LastName", Value = "Brown" },
new StringProperty { ID = 5, PersonID = 3, Name = "FirstName", Value = "James" },
new StringProperty { ID = 6, PersonID = 3, Name = "LastName", Value = "Miller" }
};
var numericProperties = new List<NumericProperty>
{
new NumericProperty { ID = 1, PersonID = 1, Name = "Age", Value = 25 },
new NumericProperty { ID = 2, PersonID = 1, Name = "Weight", Value = 50 },
new NumericProperty { ID = 3, PersonID = 2, Name = "Age", Value = 30 },
new NumericProperty { ID = 4, PersonID = 2, Name = "Weight", Value = 80 },
new NumericProperty { ID = 5, PersonID = 3, Name = "Age", Value = 32 },
new NumericProperty { ID = 6, PersonID = 3, Name = "Weight", Value = 73 }
};
数据如下:
public class Person
{
public int ID { get; set; }
public string Code { get; set; }
}
public class StringProperty
{
public int ID { get; set; }
public int PersonID { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class NumericProperty
{
public int ID { get; set; }
public int PersonID { get; set; }
public string Name { get; set; }
public int Value { get; set; }
}
var persons = new List<Person>
{
new Person { ID = 1, Code = "Person1" },
new Person { ID = 2, Code = "Person2" },
new Person { ID = 3, Code = "Person3" }
};
var stringProperties = new List<StringProperty>
{
new StringProperty { ID = 1, PersonID = 1, Name = "FirstName", Value = "John" },
new StringProperty { ID = 2, PersonID = 1, Name = "LastName", Value = "Smith" },
new StringProperty { ID = 3, PersonID = 2, Name = "FirstName", Value = "William" },
new StringProperty { ID = 4, PersonID = 2, Name = "LastName", Value = "Brown" },
new StringProperty { ID = 5, PersonID = 3, Name = "FirstName", Value = "James" },
new StringProperty { ID = 6, PersonID = 3, Name = "LastName", Value = "Miller" }
};
var numericProperties = new List<NumericProperty>
{
new NumericProperty { ID = 1, PersonID = 1, Name = "Age", Value = 25 },
new NumericProperty { ID = 2, PersonID = 1, Name = "Weight", Value = 50 },
new NumericProperty { ID = 3, PersonID = 2, Name = "Age", Value = 30 },
new NumericProperty { ID = 4, PersonID = 2, Name = "Weight", Value = 80 },
new NumericProperty { ID = 5, PersonID = 3, Name = "Age", Value = 32 },
new NumericProperty { ID = 6, PersonID = 3, Name = "Weight", Value = 73 }
};
对数值执行相同的操作:
var numericValues = from p in persons
join n in numericProperties on p.ID equals n.PersonID
group n by p.Code into g
select new
{
Code = g.Key,
Age = g.Where(n => n.Name == "Age").First().Value,
Weight = g.Where(n => n.Name == "Weight").First().Value,
};
现在将它们结合在一起:
var q = from s in stringValues
join n in numericValues on s.Code equals n.Code
select new
{
Code = s.Code,
FirstName = s.FirstName,
LastName = s.LastName,
Age = n.Age,
Weight = n.Weight
};
您可以在一个语句中执行此操作,但如果您像这样将其拆分,则更简单
实际上,我更可能在存储过程中这样做,因为存储过程更快。有很多方法可以解决这个问题。这里有一对 首先,感谢DavidG提供了类和输入数据 以下是选项1:
var query =
from p in persons
join s in stringProperties on p.ID equals s.PersonID into gss
join n in numericProperties on p.ID equals n.PersonID into gns
from fn in gss.Where(x => x.Name == "FirstName")
from ln in gss.Where(x => x.Name == "LastName")
from a in gns.Where(x => x.Name == "Age")
from w in gns.Where(x => x.Name == "Weight")
select new
{
p.Code,
FirstName = fn.Value,
LastName = ln.Value,
Age = a.Value,
Weight = w.Value,
};
下面是选项2:
var query =
from p in persons
join s in stringProperties on p.ID equals s.PersonID into gss
join n in numericProperties on p.ID equals n.PersonID into gns
let sl = gss.ToLookup(x => x.Name, x => x.Value)
let nl = gns.ToLookup(x => x.Name, x => x.Value)
from FirstName in sl["FirstName"]
from LastName in sl["LastName"]
from Age in nl["Age"]
from Weight in nl["Weight"]
select new
{
p.Code,
FirstName,
LastName,
Age,
Weight,
};
这两种方法都给出了这样的结果:
@DavidG-谢谢。:-)我给了你一个+1来创建它。英雄联盟