返回Child表的List <string>中的单列数据

时间:2018-04-06 05:00:31

标签: c# string list entity-framework linq

我有两张PK&amp; amp; FK组合。

Table: Client
int ID
nvarchar(max) ClientName
nvarchar(max) Address

Table ClientSocial
int client_ID
nvarchar(max) FB_LINK
nvarchar(max) TWITTER_LINK

我希望在List<string>中获得“FB_LINK”栏目。这是我试过的东西

var lientList= new Client();
clientList= db.Clients.Where(ab=> ab.Id==1).Select(ab=> new NewModel {
ClientName= ab.ClientName,

FacebookLinks= new List<string>
{
ab.ClientSocials.ToList(); // Here I need to change
//Things I tried
// ab.ClientSocials.Select(cs => cs.FB_LINK).ToList()
// ab.ClientSocials.Where(.. Some Condition..).Select(cs => cs.FB_LINK).ToList()
// If I use ToString() at end it works but no LIST
// ab.ClientSocials.Where(.. Some Condition..).Select(cs => cs.FB_LINK).ToList().ToString()
}
}).single();

如果需要,这里是NewModel Class

NewModel Class
{
    public string clientName {get; set; }
    public List<string> FacebookLinks { get; set; }
}

我试过,但我没有从列表中的子表中获取数据。

提前谢谢

1 个答案:

答案 0 :(得分:0)

使用Select

clientList= db.Clients
     .Where(ab => ab.Id==1)
     .Select(ab => new NewModel 
      {
         ClientName= ab.ClientName,  
         FacebookLinks= ab.ClientSocials.Select(cs => cs.FB_LINK).ToList()
      }).Single();