LINQ查询bool值传入不同的Int

时间:2012-09-07 13:08:18

标签: linq linq-to-sql

下午 我从文本框中传入一个bool值,我需要检查它是真还是假,然后添加正确的codetype(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();

我希望我已经解释过这个好了,如果不随意问的话:)提前谢谢。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

经过多次搜索,我找到了一个有效的答案......

where (hasASIN == true && a.codeType == 5) || (a.codeType == 1 || a.codeType == 2 || a.codeType == 3 || a.codeType ==4)