重构LINQ的IQueryable EX pression删除查询重复的部分 [英] refactoring LINQ IQueryable expression to remove duplicated portions of queries

查看:139
本文介绍了重构LINQ的IQueryable EX pression删除查询重复的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有冗余我想分解出一整块code一些LINQ查询。这些是加入八,New是IQueryable的,其重要的我不导致查询更早评估它比将没有重构

I have some linq queries that have redundancy I'd like to factor out a single piece of code. These are join experssions that are IQueryable, and its important I don't cause the query to be evaluated earlier than it would be without the refactoring.

下面是一个简单的查询:

Here is a simplified query:

var result = 
from T in db.Transactions
join O in db.Orders on T.OrderID equals O.OrderID
join OD in db.OrderDetails on O.OrderID equals OD.OrderID into OrderDetails
let FirstProductBought = OrderDetails.First().Select(OD => OD.Product.ProductName)
select new
{
  TransactionID = T.TransactionID,
  OrderID = O.OrderID,
  FirstProductBought = FirstProductBought
};

我想分解出是个逻辑中给出的顺序,什么是第一个产品购买。我现在用的是同样的逻辑在其他的查询。我怎么能因素它变成一个共享的方法?

What I want to factor out is th logic "given an order, what is the first product bought". I am using the same logic in other queries. How can I factor it out into a shared method?

通常,code再利用和IQueryables,我已经能够做的就是code,它需要一个IQueryable进入和产生一个IQueryable / IOrderedQueryable作为输出。有了这样的功能,我可以建立LINQ EX pressions可重复使用code仍然推迟查询,直到查询被完全构造。在这里,因为我只有一个int(订单id)我不知道如何使它发挥作用。

Generally, for code reuse and IQueryables, what I've been able to do is code that takes an IQueryable going in and produces an IQueryable/IOrderedQueryable as output. With such functions I can build up LINQ expressions with reusable code that still defer query until the query is fully constructed. Here, since I only have an int (the orderID) I'm not sure how to make it work.

感谢

推荐答案

对不起,回答我的问题,但我找到了一个很好的解决方案。我认为,尽管这取决于你想要做什么,有不同的方式来分解出不同的LINQ EX pressions没有评估的IQueryable。所以,我希望人们共享替代解决方案。

Sorry to answer my own question, but I found a good solution. I think though that depending on what you're trying to do, there are different way to factor out different LINQ expressions without evaluating the IQueryable. So I hope people share alternative solutions.

我的解决方案是创建一个视图,为提取出来的查询。我把它叫做一个观点,因为它有很多共同点与SQL视图(从LINQ客户端的角度看)。不像虽然一个SQL视图,它不能被索引或具有列持续。因此,使用这种观点成为瓶颈,这将是适合使用的实际的SQL视图。

My solution was to create a "view" for the factored out query. I call it a view because it has a lot in common with a SQL view (from the perspective of a LINQ client). Unlike a SQL view though, it cannot be indexed or have columns persisted. So using this view becomes a bottleneck, it would be appropriate to use an actual SQL view.

static public class MyDataContextExtension
{
    // The view exposes OrderSummary objects
    public class OrderSummary
    {
        public OrderID { get; set; }
        public string FirstProductListed { get; set; }
    }

    static public IQueryable<OrderSummary> OrderySummaryView(this MyDataContext db)
    {
         return (
              from O in db.Orders
              join OD in db.OrderDetails on O.OrderID equals OD.OrderID into OrderDetails
              let AProductBought = OrderDetails.First().Select(OD => OD.Product.ProductName)
              let TotalCost = OrderDetails.Aggregate(0
              select new OrderSummary()
              {
                  OrderID = OD.OrderID,
                  FirstProductListed = AProductBought.FirstOrDefault()
              };
    }
}

使用这一点,我可以分解出查询的重复部分,但有以下替换原始查询:

With this, I can factor out the duplicated portion of the query, replacing the original query with the following:

var result = 
from T in db.Transactions
join OS in db.OrderSummaryView() on T.OrderID equals OS.OrderID
select new
{
  TransactionID = T.TransactionID,
  OrderID = T.OrderID,
  FirstProductBought = OS.FirstProductListed
};

您可以想像要添加其他列...我想其中一个很酷的事情是,如果你添加额外列,但不使用它们在你的最终选择,LINQ实际上不会查询那些东西从数据库中。

You can imagine other columns being added... I think one cool thing is that if you add extra columns but don't use them in your final select, LINQ won't actually query for those things from the database.

这篇关于重构LINQ的IQueryable EX pression删除查询重复的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆