避免额外的循环,无法找到源类型int的查询模式的实现选择未找到

时间:2013-02-21 05:01:53

标签: c# linq foreach

如何在LINQ任务本身中避免额外循环和构造顺序。

GetOrderListDataContext orderListDaCtx = new GetOrderListDataContext(address);
// This line showing compiler error could not find implementation of query pattern for source type int Select not found
var orderList = from order in orderListDaCtx.Base_Purchase_GetOrderListByUser_WS(request.UserGuid, request.CountryCode, request.FromDate, request.ToDate)
                select order;
// Here how i can avoid this loop and construct order object in the LINQ itself above                   
List<Order> orders = new List<Order>();
foreach (var order in orderList)
{
    orders.Add(new Order
     {
         OrderKey = order.OrderKey,
         UserEmail = order.UserEmail,
         CreatedDate = order.CreatedDate
     });
}
return orders;

4 个答案:

答案 0 :(得分:3)

你是说这个吗?

GetOrderListDataContext orderListDaCtx = new GetOrderListDataContext(address);
var orderList = from order in orderListDaCtx.Base_Purchase_GetOrderListByUser_WS(request.UserGuid, request.CountryCode, request.FromDate, request.ToDate)
select new Order{
         OrderKey = order.OrderKey,
         UserEmail = order.UserEmail,
         CreatedDate = order.CreatedDate
}

return orderList.ToList();

答案 1 :(得分:1)

你可以链接:

var orders = orderListDaCtx.Base_Purchase_GetOrderListByUser_WS(request.UserGuid, request.CountryCode, request.FromDate, request.ToDate)
                           .Select(order => new Order
                             {
                               OrderKey = order.OrderKey,
                               UserEmail = order.UserEmail,
                               CreatedDate = order.CreatedDate
                             })
                           .ToList();
return orders;

答案 2 :(得分:1)

  

此行显示编译器错误无法找到实现   source type int的查询模式选择未找到

您的主要问题来自您的存储过程。请参阅:Linq Stored Procedure Issue- Returning an int

答案 3 :(得分:0)

请参阅LINQ Error

  

无法找到源类型“Your.Type”的查询模式的实现。找不到“选择”。考虑明确指定范围变量的类型

orderListDaCtx.Base_Purchase_GetOrderListByUser_WS(request.UserGuid, request.CountryCode, request.FromDate, request.ToDate)可能会返回“IEnumerable”类型,但LINQ需要IEnumerable<T>。你必须执行显式转换:

var orderList = from Order order in orderListDaCtx.Base_Purchase_GetOrderListByUser_WS(request.UserGuid, request.CountryCode, request.FromDate, request.ToDate)
                select order;

我认为,Base_Purchase_GetOrderListByUser_WS会返回Order的集合。