簡體   English   中英

使用Lambda表達式查詢將字符串列表轉換為逗號分隔的字符串

[英]Convert list of string into a comma separated string using lambda expression query

我附加了完整的查詢,其中ps.productId返回字符串列表,並且我希望所有字符串以逗號分隔為單個字符串,例如“ a,b,c”。如何使用lambda表達式實現這一點!

ProductIDs = string.Join(",", ps.ProductID),
ProductIDs = string.Join(",", _DataContext.ProductSelectionEntity.Where(x => x.BillingId == bill.Id).Select(x => x.ProductID).ToList())             

ps.productIds將返回List<string> ,我希望它以字符串格式"1,2,3,4"

var results = (from uastatus in _DataContext.UaStatusEntity
               where uastatus.IsUaComplete == false
               join client in _DataContext.Client on uastatus.ClientID equals client.ClientID
               where client.ClientStatus == "Active" && 
                     client.IsEnrolledNHCR.HasValue && 
                     client.IsEnrolledNHCR.Value
               join ps in _DataContext.ProductSelectionEntity on bill.ClientId equals ps.ClientID
               where bill.Id == ps.BillingId
               select new PendingUA
                          {
                               ClientId = client.ClientID,
                               ClientRelationship = client.ClientRelationship,
                               ClientName = client.ClientName,
                               EIN = client.EIN,     
                               ProductIDs = string.Join(",", ps.ProductID),
                               ProductIDs = string.Join(",",_DataContext.ProductSelectionEntity.Where(x => x.BillingId == bill.Id).Select(x => x.ProductID).ToList())             
                          }).Distinct().ToList();

錯誤信息

使用string.Join之前必須從數據庫中提取值。

var resultsFromDB = (from uastatus in _DataContext.UaStatusEntity
               where uastatus.IsUaComplete == false
               join client in _DataContext.Client on uastatus.ClientID equals client.ClientID
               where client.ClientStatus == "Active" && 
                     client.IsEnrolledNHCR.HasValue && 
                     client.IsEnrolledNHCR.Value
               join ps in _DataContext.ProductSelectionEntity on bill.ClientId equals ps.ClientID
               where bill.Id == ps.BillingId
               select new 
                          {
                               ClientId = client.ClientID,
                               ClientRelationship = client.ClientRelationship,
                               ClientName = client.ClientName,
                               EIN = client.EIN,     
                               ProductID = ps.ProductID,

                          }).ToList();
var results = (from value in resultsFromDB
              let ProductIDS = string.Join(",", resultsFromDB.Where(x => x.ClientId == value.ClientId ).Select(x => x.ProductID).ToList())
               select new PendingUA
                          {
                               ClientId = value.ClientID,
                               ClientRelationship = value.ClientRelationship,
                               ClientName = value.ClientName,
                               EIN = value.EIN,     
                               ProductIDS = ps.ProductIDS ,

                          }).Distinct().ToList();

為了獲得更好的性能,請使用linq group by

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM