LINQ to SQL的嵌套前pression [英] Linq To Sql Nested Expression

查看:186
本文介绍了LINQ to SQL的嵌套前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这当然pression来获取所有产品:

 防爆pression<&Func键LT;产品,ProductInCityDto>> GetAllProducts
{
    得到
    {
        返回产品=>新ProductInCityDto
        {
             产品名称= product.ProductName,
             CITYNAME = product.Store.City.Name,
             国家或地区名称= product.Store.City.Country.Name,
             ProductAvgPrice = CalculateAvg(产品)
             。
             。
             。
        }
    }
}

我使用的功能CalculateAvg来计算产品的平均价格。计算是分离的功能,因为我使用这个code在几个地方。
然而,这种方法会导致在数据库中的多个电话。
是否有可能更换功能CalculateAvg使用LINQ防爆pression,以只有一个调用数据库?

编辑:

功能CalculateAvg看起来是这样的:

 公共静态小数CalculateAvg(对象TEMPOBJ)
{
   产品的obj = TEMPOBJ的产品;
   返回Convert.ToDecimal
          (obj.Sales.Where(N =>!n.type = 1)
                 。平均(N => n.Price)
          );
}


解决方案

这困难的部分是完全没有转换到Linq的前pression。困难的部分是前pression的调用。这是很难的。但最糟糕的是,它不是类型安全的,并涉及一些类似于反射。

实际魔法的一部分是IQueryable.Provider.Execute(前pression)。

您当然可以试试这个...但它是没有测试手段。更何况,pretty的fugly。

 防爆pression<&Func键LT;产品,ProductInCityDto>> GetAllProducts
{
    得到
    {
        返回产品=>新ProductInCityDto
        {
             产品名称= product.ProductName,
             CITYNAME = product.Store.City.Name,
             国家或地区名称= product.Store.City.Country.Name,
             //一个黑客位要挟提供了...
             ProductAvgPrice = product.Sales
                    .AsQueryable.Provider
                    .Execute<双>(CalculateAvg(产品),防爆pression.Constant(产品的typeof(产品)))
             。
             。
             。
        }
    }
}公共防爆pression<&Func键LT;产品,双>> CalculateAvg()
{
    返回产品=> product.Sales.Where(N =>!n.type = 1)。平均(N => n.Price);
}

I am using this expression to get all products:

Expression<Func<Product, ProductInCityDto>> GetAllProducts
{
    get
    {
        return product => new ProductInCityDto
        {
             ProductName = product.ProductName,
             CityName = product.Store.City.Name,
             CountryName = product.Store.City.Country.Name,
             ProductAvgPrice =  CalculateAvg(product)
             . 
             . 
             .  
        }
    }
}

I am using the function CalculateAvg to calculate the product's average price. The calculation is in separated function because I'm using this code in several places. However, this approach causes multiple calls to the database. Is it possible to replace the function CalculateAvg with Linq Expression, in order to have only one call to the DB?

Edit:

The function CalculateAvg looks something like this:

public static decimal CalculateAvg(object tempObj)
{
   Product obj = tempObj as Product; 
   return Convert.ToDecimal
          (obj.Sales.Where(n => n.type != 1)
                 .Average(n=>n.Price)
          );
}

解决方案

The difficult part of this isn't the conversion to the Linq expression. The difficult part is the invoking of the Expression. Which is hard. But worst of all, it isn't typesafe, and involves something akin to reflection.

The actual magic part is IQueryable.Provider.Execute(Expression)..

You can certainly try this...but it is by no means tested. Not to mention, pretty fugly.

Expression<Func<Product, ProductInCityDto>> GetAllProducts
{
    get
    {
        return product => new ProductInCityDto
        {
             ProductName = product.ProductName,
             CityName = product.Store.City.Name,
             CountryName = product.Store.City.Country.Name,
             //Bit of a hack to coerce the Provider out...
             ProductAvgPrice = product.Sales
                    .AsQueryable.Provider
                    .Execute<double>(CalculateAvg(product), Expression.Constant(product, typeof (Product)) ),
             . 
             . 
             .  
        }
    }
}

public Expression<Func<Product, double>> CalculateAvg()
{
    return product => product.Sales.Where(n => n.type != 1).Average(n=>n.Price);
}

这篇关于LINQ to SQL的嵌套前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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