简单的LINQ搜索查询,添加其他参数

时间:2012-06-20 10:26:24

标签: c# .net linq

下午,我正在尝试进行简单搜索,该搜索适用于产品标题。但是我需要为搜索添加其他参数。我有以下代码的基础,至少我认为应该如此。我已经注释掉了其他项目

有人可以提供一些指导,因为我被困在它上面。

var query = from a in dc.aProducts
            join t in dc.bProducts on a.sku equals t.sku
            where SqlMethods.Like(t.title, "%" + productName + "%")

            //Compare Prices
            //&& (p => (double)p.Price >= priceFrom && (double)p.Price <= priceTo)
            //Product SKU
            //t.sku == productSku
            //Product Brand
            //t.brand == productBrand
            //a.asin == productAsin
            //a.Live == IsLive

非常感谢,所有的帮助都非常感谢。

2 个答案:

答案 0 :(得分:3)

我很想做这样的事情:

bool comparePrices = true;

// Join tables and get all products into query
var query = from a in dc.aProducts
            join t in dc.bProducts on a.sku equals t.sku
            select a;

// Now go through each search criteria if needed in order to filter down
if(comparePrices)
    query = query.Where(p => (double)p.Price >= priceFrom 
                          && (double)p.Price <= priceTo);

if(!string.IsNullOrEmpty(productSku))
{
    query = query.Where(t.sku == productSku);
}

每次有条件地向原始查询添加过滤器时。

答案 1 :(得分:0)

有人给了我很好的答案:

var filters = new List<Func<f_results, bool>>();

if (comparePrices) filters.add((p => (double)p.Price >= priceFrom && (double)p.Price <= priceTo);
if (productQuery) filters.add(p => p.sku == productSku);

result = query.find (p => filters.All(filter => filter(p)));
相关问题