制作了一些模拟示例,但这也适用于 LinqToMySql。
class user {
public string name {get;set;}
public int id {get;set;}
}
class member_detail {
public int user_id {get;set;}
public string prefered {get;set;}
}
class city_master{
public int code {get;set;}
public string name {get;set;}
}
void Main()
{
var users = new List<user>();
users.Add(new user(){name = "Mary",id = 1});
users.Add(new user(){name = "John",id=2});
var details = new List<member_detail>() ;
details.Add(new member_detail(){user_id=1,prefered="1,2,3"});
details.Add(new member_detail(){user_id=2,prefered="3,5"});
var cities = new List<city_master>();
cities.Add(new city_master(){code =1,name="Moscow"});
cities.Add(new city_master(){code =2,name="London"});
cities.Add(new city_master(){code =3,name="Paris"});
cities.Add(new city_master(){code =4,name="Rome"});
cities.Add(new city_master(){code =5,name="Madrid"});
users.Select(u=>new {u.name,cities=
details.Where(d=>d.user_id==u.id)
.SelectMany(d=>d.prefered.Split(','))
.Join(cities,c=>c,d=>d.code.ToString(),(a,b)=>new {b.name})}).Dump();
}