4

我如何避免额外循环并在 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

4 回答 4

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();
于 2013-02-21T05:06:03.660 回答
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;
于 2013-02-21T05:29:48.017 回答
1

显示编译器错误的这一行找不到源类型 int 的查询模式的实现 未找到选择

您的主要问题来自您的存储过程。参考:Linq 存储过程问题 - 返回一个 int

于 2013-02-21T05:31:57.787 回答
0

请参阅LINQ 错误

找不到源类型“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.

于 2013-02-21T07:45:29.280 回答