Linq List列表中的字符串

时间:2015-11-03 09:46:02

标签: c# linq

我试图做一个单独的linq语句,以下工作但希望在一个语句中完成。

public class ClientProducts
    {
        public string To { get; set; }

        public string ClientFullName { get; set; }

        public string ClientFirstName { get; set; }

        public string ProductNames{ get; set; }
}

 var list =
                    clients.Select(
                        client =>
                            new ClientProducts()
                            {
                                To = client.TelephoneMobile,
                                ClientFirstName = client.FirstName,
                                ClientFullName = client.FullName,
                                //ProductNames= client.Products.Select(p=>p.Name)<-this is what I want
                            }).ToList();

                string productName= string.Empty;
                foreach (var client in clients)
                {
                    foreach (var p in client.Products)
                    {
                        productName+= Name+ ",";
                    }
                }

1 个答案:

答案 0 :(得分:2)

替换

//ProductNames= client.Products.Select(p=>p.Name)

ProductNames = string.Join(",", client.Products.Select(p=>p.Name))