结果与空 LINQ 查询的并集

发布于 2025-01-04 09:45:49 字数 937 浏览 1 评论 0原文

在数据库中,我有一些产品和价格。 我需要寻找价格限制。问题是可能有几个限制。例如,0 - 500 $ 1500 - $ 2000

如何合并此 LINQ 查询

var products = from product in db.Products
where product.Price >= 0 and product.Price <= 500

var products = from product in db.Products
where product.Price >= 1500 and product.Price <= 2000

我的变体:

  IQueryable<Deal> allResults = null;
            for(var i = 0; i < price.Length - 1; i = i + 2)
             {
              decimal start,end;
              decimal.TryParse(price[i], out start);
              decimal.TryParse(price[i+1], out end);

              var tempResults = from product in query
                         where (product.DiscountPrice >= start && product.DiscountPrice <=                    end)
                       select product;
                    tempResults.Union(allResults);
                }

如何创建一个空查询来连接?

In the database, I have some of the products and prices.
I need to search for the price limits. The problem is that there may be several limits. For example, 0 - 500 $ 1500 - $ 2000

How to merge this LINQ queries

var products = from product in db.Products
where product.Price >= 0 and product.Price <= 500

var products = from product in db.Products
where product.Price >= 1500 and product.Price <= 2000

My varriant:

  IQueryable<Deal> allResults = null;
            for(var i = 0; i < price.Length - 1; i = i + 2)
             {
              decimal start,end;
              decimal.TryParse(price[i], out start);
              decimal.TryParse(price[i+1], out end);

              var tempResults = from product in query
                         where (product.DiscountPrice >= start && product.DiscountPrice <=                    end)
                       select product;
                    tempResults.Union(allResults);
                }

how to create an empty query to join ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够爱げ你 2025-01-11 09:45:49

一种方法是合并条件:

var products = from product in db.Products
where (product.Price >= 0 and product.Price <= 500)||product.Price >= 1500 and product.Price <= 2000

另一种方法是使用 UnionConcat

var products = (from product in db.Products
                     where product.Price >= 0 and product.Price <= 500)
         .Union(from product in db.Products
                     where product.Price >= 1500 and product.Price <= 2000);

调整代码可以做到:

IQueryable<Deal> allResults = null;
for(var i = 0; i < price.Length - 1; i = i + 2)
{
    decimal start,end;
    decimal.TryParse(price[i], out start);
    decimal.TryParse(price[i+1], out end);

    var tempResults = from product in db.Products
                      where (product.DiscountPrice >= start && product.DiscountPrice <= end)
                      select product;
    if(allResults!=null)
       allResults=allResults.Union(tempResults);
    else
       allResults=tempResults;
}

您不需要空查询来合并,您可以添加一个特殊情况来测试这是否是第一个查询。

但我不太喜欢该代码,因为它混合了两个问题:查询的构造和查询的合并。所以我把它分成两部分:

IEnumerable<Tuple<Decimal,Decimal>> ParsePrices(string[] rawPrices)
{
    for(var i = 0; i < price.Length - 1; i = i + 2)
    {
       decimal start=decimal.Parse(price[i]);
       decimal end  =decimal.Parse(price[i+1]);

       yield return Tuple.Create(start, end);
}

var prices=ParsePrices(rawPrices);
IEnumerable<IQueryable<Deal>> partialQueries=prices.Select( interval=>
       from product in db.Products
       where (product.DiscountPrice >= interval.Item1 && product.DiscountPrice <= interval.Item2)
       select product;);
var mergedQuery=partialQueries.Aggregate((q1,q2)=>q1.Union(q2));

One way is to merge the conditions:

var products = from product in db.Products
where (product.Price >= 0 and product.Price <= 500)||product.Price >= 1500 and product.Price <= 2000

Another is to use Union or Concat:

var products = (from product in db.Products
                     where product.Price >= 0 and product.Price <= 500)
         .Union(from product in db.Products
                     where product.Price >= 1500 and product.Price <= 2000);

Adapting your code on can do:

IQueryable<Deal> allResults = null;
for(var i = 0; i < price.Length - 1; i = i + 2)
{
    decimal start,end;
    decimal.TryParse(price[i], out start);
    decimal.TryParse(price[i+1], out end);

    var tempResults = from product in db.Products
                      where (product.DiscountPrice >= start && product.DiscountPrice <= end)
                      select product;
    if(allResults!=null)
       allResults=allResults.Union(tempResults);
    else
       allResults=tempResults;
}

You don't need an empty query in to merge with, you can just add a special case that tests if this is the first query.

But I don't like that code much, because it mixes two concerns: The construction of the queries, and the merging of the queries. So I'd split it in two:

IEnumerable<Tuple<Decimal,Decimal>> ParsePrices(string[] rawPrices)
{
    for(var i = 0; i < price.Length - 1; i = i + 2)
    {
       decimal start=decimal.Parse(price[i]);
       decimal end  =decimal.Parse(price[i+1]);

       yield return Tuple.Create(start, end);
}

var prices=ParsePrices(rawPrices);
IEnumerable<IQueryable<Deal>> partialQueries=prices.Select( interval=>
       from product in db.Products
       where (product.DiscountPrice >= interval.Item1 && product.DiscountPrice <= interval.Item2)
       select product;);
var mergedQuery=partialQueries.Aggregate((q1,q2)=>q1.Union(q2));
浪推晚风 2025-01-11 09:45:49

您可以使用 Enumerable.Empty().AsQueryable() 创建空查询。

您的代码中存在的问题:Union 不会更改您作为参数传递给它的任何序列;相反,它创建一个返回的新序列。因此,您应该将 tempResults.Union(allResults) 操作的结果分配给 allResults

这是一些示例代码(未经测试):

IQueryable<Deal> filteredResults = Enumerable.Empty<Deal>().AsQueryable();

for (int i = 0; i < price.Length - 1; i += 2)
{
    decimal start, end;
    decimal.TryParse(price[i], out start);
    decimal.TryParse(price[i+1], out end);

    var tempResults = from product in db.Products
                      where product.DiscountPrice >= start && product.DiscountPrice <= end
                      select product;

    filteredResults = filteredResults.Union(tempResults);
}

You can create an empty query using Enumerable.Empty<T>().AsQueryable().

An issue you have in your code: Union does not alter any of the sequences that you pass to it as parameters; rather, it creates a new sequence which it returns. Thus, you should be assigning the result of your tempResults.Union(allResults) operation to allResults.

Here is some sample code (not tested):

IQueryable<Deal> filteredResults = Enumerable.Empty<Deal>().AsQueryable();

for (int i = 0; i < price.Length - 1; i += 2)
{
    decimal start, end;
    decimal.TryParse(price[i], out start);
    decimal.TryParse(price[i+1], out end);

    var tempResults = from product in db.Products
                      where product.DiscountPrice >= start && product.DiscountPrice <= end
                      select product;

    filteredResults = filteredResults.Union(tempResults);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文