English 中文(简体)
Linq To Sql Where Or operator
原标题:

I need to create a query which checks if a field (string) contains one or more words supplied at run time.

Basically I need to be able to ask a WhereOr question. This seems like it should be a common issue when dealing with LinqToSql.

I found the following reference but can t make sense out of it - and have no idea how to use it in my project.

i ve tried the following loop:

        var query = from d in context.Domains select d;
        for (int i = 0; i < words.Length; i++)
        {
            query = query.Where(d => d.Name.Contains(words[i]));
        }

but this builds a SQL query with WHERE AND Clauses NOT Where OR

最佳回答

I use PredicateBuilder for such things.

The predicate construction looks like this:

     var query = from d in context.Domains select d;
     var predicate = PredicateBuilder<Domains>.False();

     for (int i = 0; i < words.Length; i++)
        {
            predicate = predicate.Or(d => d.Name.Contains(words[i]));
        }
    query = query.Where(predicate);
问题回答

暂无回答




相关问题
using the where clause + new constraint with args?

I have a piece of code that looks like this: public static T CreateSomething<T>(SomeType a) where T : SomeMicrosoftBaseClass, new() { var newElement = new T { SomeProperty = a}; ...

Strange .Where() behaviour. Somebody has an explanation?

Original I don t get why the Where() clause doesn t give me the right results in the last example. It isn t any different is it? Why does C# behaves differently? transactions = IEnumerable<...

Linq To Sql Where Or operator

I need to create a query which checks if a field (string) contains one or more words supplied at run time. Basically I need to be able to ask a WhereOr question. This seems like it should be a ...

C# Beginner: Where has my IList.Where() method gone?

I ve got another simple one (I think) that s stumping me. I have written a method in one of my controls that gets the latest version of a file in a CMS given it s filename (i.e. regardless of what ...

sql simple query

Yesterday my friend asked me a question about this query: select * from user where 1=1 I said that the query is incorrect, but he said it s correct. I don t understand how this query can be correct....

Codeigniter WHERE on "AS" field

I have a query where I need to modify the selected data and I want to limit my results of that data. For instance: SELECT table_id, radians( 25 ) AS rad FROM test_table WHERE rad < 5 ORDER BY rad ...

SEARCH several Tables IN SQL

I m trying to search several tables at once for a search term. My query is: SELECT item.ItemID FROM Inventory.Item item JOIN Inventory.Category catR // each item can be in several ...

热门标签