0

下午,我从一个文本框中传入一个 bool 值,我需要检查它是真还是假,然后添加正确的代码类型 (int) 以供搜索。

例如

if (hasASIN == true)
{
    int show = 5;
}
else {
    int show = 1, 2, 3, 4 (these are the other code types)
}

然后我需要在我的 LINQ 语句中添加一个 where 子句。

where a.codeType == show (need to have this maybe in a || (or) bit so i can use the other code types)

下面是我现有的 LINQ 代码

 var query = from a in dc.aboProducts
             join t in dc.tweProducts on a.sku equals t.sku

             where (string.IsNullOrEmpty(productSku) || productSku == t.sku)
             where (string.IsNullOrEmpty(productAsin) || productAsin == a.asin)
             where (string.IsNullOrEmpty(productName) || t.title.Contains(productName))
             where (string.IsNullOrEmpty(productBrand) || t.brand.Contains(productBrand))
             where a.amzPrice >= priceFrom && a.amzPrice <= (priceTo)
             where a.amzLive == isLive 

             select new GetProducts
             {
                productid = Convert.ToInt32(t.id),
                sku = t.sku,
                title = t.title,
                tweprice = Convert.ToString(t.twePrice),
                stock = Convert.ToInt32(t.stock),
                asin = a.asin,
                amzprice = Convert.ToString(a.amzPrice),
                amzlive = Convert.ToBoolean(a.amzLive),
                lastupdated = Convert.ToDateTime(t.lastUpdated)
             };
  return query.ToList();

我希望我已经解释过了,如果不能随意询问:) 提前谢谢。

4

2 回答 2

4

我会int show改为IEnumerable<int> show. 然后你可以做你的if语句

IEnumerable<int> show;

if (hasASIN == true)
{
     show = new[] {5};
}
else 
{
     show = new[] {1, 2, 3, 4};
}

var query = ....

在您的查询中,您可以拥有where show.Contains(a.codeType)

于 2012-09-07T13:33:20.287 回答
0

经过大量搜索,我找到了一个很好的答案......

where (hasASIN == true && a.codeType == 5) || (a.codeType == 1 || a.codeType == 2 || a.codeType == 3 || a.codeType ==4)
于 2012-09-07T15:46:37.430 回答