2015-08-13 166 views
2

我有一個產品表和一個活動表。每個產品都是由不同的活動組成的。我想要顯示產品名稱,價格等以及所有活動(即所有在一列中,其是使用LINQ 在DataGrid該產品的一部分的活動),下面是我的查詢linq查詢返回一個列表中的字符串

using (bungeedbEntities context = new bungeedbEntities()) 
{ 
    var bookingData = from con in context.bookings 
    join agn in context.agents on con.main_agent_id equals agn.code 

    select new POS_LINK.BusinessObjects.Bookings 
    { 
     Product = con.product_name, 
     Activity = String.Join(",", (from con1 in context.bookings 
            join acp in context.booking_activity on con1.code equals acp.booking_code 
            join agn in context.agents on con1.main_agent_id equals agn.code 
            join act in context.activities on acp.activity_code equals act.code 
            select act.name).ToArray()), 
    ReservationCode = con.main_agent_voucher, 
    CostOfSale = 0.00M, 
    DateOfActivity = (DateTime)con.date_of_activity, 
    Notes = con.notes, 
    Quantity = (int)con.pax, 
          Child_quantity = 0, 
          Child_cost_percentage = 0, 
          CostPerPerson = 0.00M, 
          SubAgentRef = "56789", 
          SubAgentName = con.sub_agent_name, 
          ClientName = con.client_name, 
          MainAgent = agn.agent_name, 
          Consultant2 = con.sub_agent_consultant          
         }; 
    return bookingData.ToList(); 

在運行我碰到下面的錯誤 - LINQ到實體無法識別方法「系統。 String Join(System.String,System.String [])'方法,並且此方法不能轉換爲存儲表達式。

我似乎已經江郎才盡的人有更好的解決方案,這會救我脫離了很多頭部劃傷

+0

'String.Join'不會在Linq內部轉換爲SQL,如果是這樣的話,那麼您將爲每個結果行執行一個子選擇,而這個行根本不是表現性的。考慮分組結果並在稍後的步驟中進行連接。 –

回答

0

你需要做的兩個步驟:第一步,選擇數據,而第二步將其轉換爲一個字符串string.Join

var bookingData = (from con in context.bookings 
    join agn in context.agents on con.main_agent_id equals agn.code 
    select new { // Construct an anonymous type with the relevant parts 
     Product = con.product_name, 
     ActivityData = (from con1 in context.bookings 
         join acp in context.booking_activity on con1.code equals acp.booking_code 
         join agn in context.agents on con1.main_agent_id equals agn.code 
         join act in context.activities on acp.activity_code equals act.code 
         select act.name), 
     ReservationCode = con.main_agent_voucher, 
     DateOfActivity = (DateTime)con.date_of_activity, 
     Notes = con.notes, 
     Quantity = (int)con.pax, 
     SubAgentName = con.sub_agent_name, 
     ClientName = con.client_name, 
     MainAgent = agn.agent_name, 
     Consultant2 = con.sub_agent_consultant          
    }).AsEnumerable() // Bring this into memory 
    .Select(p => new POS_LINK.BusinessObjects.Bookings { 
     Product = p.Product, 
     Activity = string.Join(", ", p.ActivityData.ToArray()), 
     ReservationCode = p.ReservationCode, 
     CostOfSale = 0.00M, 
     Notes = p.Notes, 
     Quantity = p.Quantity, 
     Child_quantity = 0, 
     Child_cost_percentage = 0, 
     CostPerPerson = 0.00M, 
     SubAgentRef = "56789", 
     SubAgentName = p.SubAgentName, 
     ClientName = p.ClientName, 
     MainAgent = p.MainAgent, 
     Consultant2 = p.Consultant2 
    }); 
return bookingData.ToList(); 

這裏的想法很簡單:首先,你構造一個具有所有相關信息,包括acc.name秒的枚舉匿名類型,那麼您可以通過調用它放在內存中AsEnumerable(),最後構建你的POS_LINK.BusinessObjects.Bookings使用LINQ-to-Object的對象,它理解string.Join

+0

thanx @dasblinkenlight你救了我的一天....它的工作 – tyndo