English 中文(简体)
如何写成 LINQ 条款查询, 哪些也会像操作员一样有效?
原标题:How to write LINQ IN clause query which will work as LIKE operator as well?

如何在选中的 sql 查询后写入 LINQ 查询 :

string brandid="1,2,3"
string bodystyleid="1,2,3"
-------------------
-----------------

select * from car
where brandid in (brandid)
and bodystyleid in (brandid)
----------------------
-------------------

My specific requirement is that if brandid or bodystyleid is blank(if user does not select any checkbox of a particular search option) query should return all record for that particular where condition.

请指引我

谢谢

保罗 保罗

最佳回答

如果您没有指定要返回的所有项目,为了满足要返回所有项目的要求,您需要检查列表是否为空。

var brands = brandid.Split( , ).Select(x => Int32.Parse(x));
var styles = bodystyleid.Split( , ).Select(x => Int32.Parse(x));

var result = from c in car
             where (!brands.Any() || brands.Contains(c.brandid))
                  && (!styles.Any() || styles.Contains(c.bodystyleid))
             select c;

(类似于 sgmoore s 溶液, 但包括未指定品牌/ 风格的检查)

我还没有实际检查它是如何被转换回 SQL - 使用旗帜来显示是否有值可能更有效率 :

var brands = ....;   // As above
bool anyBrands = brands.Any()
var result = from c in car
             where (!anyBrands || brands.Contains(c.brandid))
               .....
问题回答

体型是用来检查品牌还是体型? (我假定是体型,但写了查询来与问题中的查询吻合(brandid))

作为开始,你可以做:

var results = (from c in car
               where c.brandid.Contains(brandid)
               && c.bodystyleid.Contains(brandid)
               select c).ToList();
var brandids     = brandid    .Split( , ).Select(n => int.Parse(n)).ToList();
var bodyStyleids = bodystyleid.Split( , ).Select(n => int.Parse(n)).ToList();


var results =
    (from c in car where 
      brandids.Contains(c.brandid) && 
      bodyStyleids.Contains(c.bodystyleid) 
     select c
   ).ToList();

您拥有的 Id 作为逗号分隔符的字符串, 您需要它们像您 list 表格的 Car 表格的 > Id 一样, 像 list 一样的收藏, 所以, 如果 brandidd 列是 int 那么 > brandids 必须是 list< long> , 那么你可以这样做。

var results = (
    from c in cars
    where brandids.Contains(c.brandid) && bodystyleid.Contains(c.bodystyleid) 
    select c).ToList();




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...