A表,字段:AID,CLASS
B表,字段 :BID,BNAME,AID
A表数据
1 班级1
2 班级2
B表数据
1 学生1 1
2 学生2 1
3 学生3 2
4 学生4 2
我想得到
CLASS NAME
班级1 学生1,学生2
班级2 学生3,学生4
这样怎么联合
7 个解决方案
#1
namespace ConsoleApplication1
{
public class A
{
public int AID { get; set; }
public string Class { get; set; }
}
public class B
{
public int BID { get; set; }
public string BName { get; set; }
public int AID { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<A> A = new List<A>()
{
new A(){ AID = 1, Class="班级1" },
new A(){ AID = 2, Class="班级2" },
};
List<B> B = new List<B>()
{
new B(){ BID = 1 , BName = "学生1", AID=1 },
new B(){ BID = 2 , BName = "学生2", AID=1 },
new B(){ BID = 3 , BName = "学生3", AID=2 },
new B(){ BID = 4 , BName = "学生4", AID=2 },
};
var result = B.GroupBy(x => x.AID).Select(x => new { Key = x.Key, Value = string.Join(",", B.Where(y => y.AID == x.Key).Select(y => y.BName)) });
foreach (var item in result)
{
Console.WriteLine(item);
}
}
}
}
#2
然后关联一下就出来了。
var lastResult = from p in A
from q in B.GroupBy(x => x.AID).Select(x => new { Key = x.Key, Value = string.Join(",", B.Where(y => y.AID == x.Key).Select(y => y.BName)) })
where p.AID == q.Key
select new
{
CLASS = p.Class,
Name = q.Value,
};
#3
LINQ to Entities 不识别方法“System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])”,因此该方法无法转换为存储表达式。
#4
控制台项目,.net framework4.0
#5
LINQ to Entities里面不能用string.join吗
#7
这个是Tostring报错的,我知道怎么解决
#1
namespace ConsoleApplication1
{
public class A
{
public int AID { get; set; }
public string Class { get; set; }
}
public class B
{
public int BID { get; set; }
public string BName { get; set; }
public int AID { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<A> A = new List<A>()
{
new A(){ AID = 1, Class="班级1" },
new A(){ AID = 2, Class="班级2" },
};
List<B> B = new List<B>()
{
new B(){ BID = 1 , BName = "学生1", AID=1 },
new B(){ BID = 2 , BName = "学生2", AID=1 },
new B(){ BID = 3 , BName = "学生3", AID=2 },
new B(){ BID = 4 , BName = "学生4", AID=2 },
};
var result = B.GroupBy(x => x.AID).Select(x => new { Key = x.Key, Value = string.Join(",", B.Where(y => y.AID == x.Key).Select(y => y.BName)) });
foreach (var item in result)
{
Console.WriteLine(item);
}
}
}
}
#2
然后关联一下就出来了。
var lastResult = from p in A
from q in B.GroupBy(x => x.AID).Select(x => new { Key = x.Key, Value = string.Join(",", B.Where(y => y.AID == x.Key).Select(y => y.BName)) })
where p.AID == q.Key
select new
{
CLASS = p.Class,
Name = q.Value,
};
#3
LINQ to Entities 不识别方法“System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])”,因此该方法无法转换为存储表达式。
#4
控制台项目,.net framework4.0
#5
LINQ to Entities里面不能用string.join吗
#6
#7
这个是Tostring报错的,我知道怎么解决