linq查询从列表中返回一个字符串

时间:2015-08-13 14:39:41

标签: c# linq

我有一个产品表和一个活动表。每个产品都由不同的活动组成。我想要的是显示产品名称,价格等以及所有活动(即一列中的所有活动)是使用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 to Entities无法识别方法' System.String Join(System.String,System.String [])'方法,并且此方法无法转换为商店表达式。

我似乎已经没有想法,任何有更好解决方案的人都会让我免受很多伤害

1 个答案:

答案 0 :(得分: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()将其强制进入内存,最后构造使用LINQ-to-Object的POS_LINK.BusinessObjects.Bookings对象,它理解string.Join