如何从 linq 返回特定列表结果到实体
专家您好 我当前编写此代码:
public IList<TResult> GetCustomQuery<TResult>(int orderID, Func<Order, TResult> selector)
{
using(RepositoryDataContext = new NorthwindEntities())
{
IList<TResult> res = (from od in RepositoryDataContext.Order_Details
join o in RepositoryDataContext.Orders
on od.OrderID equals o.OrderID
join p in RepositoryDataContext.Products
on od.ProductID equals p.ProductID
join c in RepositoryDataContext.Customers
on o.CustomerID equals c.CustomerID
where o.OrderID > orderID
select new
{
o.OrderID,
od.UnitPrice,
od.Quantity,
p.ProductName,
c.CompanyName
}).Select<Order, TResult>(selector).ToList();
}
}
我想以特定格式(TResult)将 linq 的结果返回到实体。 由于连接的存在,使用 Lambda 表达式编写此代码很困难。 此行有例外:选择 订单,TResult> (选择器) 我该如何解决这个问题? 谢谢
Hi Experts
I currenly write this code:
public IList<TResult> GetCustomQuery<TResult>(int orderID, Func<Order, TResult> selector)
{
using(RepositoryDataContext = new NorthwindEntities())
{
IList<TResult> res = (from od in RepositoryDataContext.Order_Details
join o in RepositoryDataContext.Orders
on od.OrderID equals o.OrderID
join p in RepositoryDataContext.Products
on od.ProductID equals p.ProductID
join c in RepositoryDataContext.Customers
on o.CustomerID equals c.CustomerID
where o.OrderID > orderID
select new
{
o.OrderID,
od.UnitPrice,
od.Quantity,
p.ProductName,
c.CompanyName
}).Select<Order, TResult>(selector).ToList();
}
}
I want to return result of my linq to entities in specific format(TResult).
writing this code using Lambda Expression is hard because of joins.
this line has exception :Select < Order, TResult > (selector)
how I can Fix that?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要创建匿名类型,而是创建 POCO 或在您的情况下创建订单
Instead of creating an Anonymous type, create a POCO or in your case an Order
在倒数第二行中:尝试在 Select() 之前调用 ToList() 。像这样:
In your second last line: Try to call ToList() before the Select(). Like this:
是否可以迭代“res”并返回 TResult 列表?
Is it possible Iterate "res" and return a list of TResult?