0

I have three fields in a form on the view:

[Textfield] : Name  
[Dropdown]  : Sector  
[Dropdown]  : Country  

My requirement is to filter the results via these three keywords in the controller method, and also order the result first by received date and then by name.

There are so many possibilities, eg: if Name matches and Sector matches do filtering etc.

I can do it with if else or with any other cumbersome way, but how can I do this in LINQ in one statement. I need to return a list.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
melbourne-geek
  • 377
  • 1
  • 11
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 15 '13 at 06:06
  • Could you give more information or code you have tried – cuongle Jan 15 '13 at 09:51
  • Thanks everyone who answered my question. Found the answer what i was looking for below and hope it will help others. http://blog.tech-cats.com/2008/01/using-optional-parameters-in-sql-server.html – melbourne-geek Jan 29 '13 at 23:35

3 Answers3

1

If you need just simple search to find out that some of your columns contains specified string, try something like this.

// dc is some datacontext
var query = dc.MyEntities.Where(x=> x.Name.Contains(inputName) ||
                                    x.Sector.Contains(inputSector) ||
                                    x.Contry.Contains(inputCountry))
                         .OrderBy(x=> x.receivedDate)
                         .ThenBy(x=> x.Name);
var resultList = query.ToList();

Also check (set) the collation of your database to do case sensitive (CS) or case insensitive (CI) search - according to your needs. However to do case insensitive search on CS collation you will need extend the code above with ToUpper().

x.Name.ToUpper().Contains.(inputName.ToUpper());

But beware, it won't pass the Turkey test :-) - there is something weird with uppercasing their i and lowercasing I.

Here is also interesting resource on case insensitive .Contains(string): Case insensitive 'Contains(string)'

If you need some sophisticated full text search. I would look for:

Community
  • 1
  • 1
mipe34
  • 5,596
  • 3
  • 26
  • 38
0

Have a look at this solution and see if it helps you to achieve your desired goal: search keywords using linq

Community
  • 1
  • 1
Karey Powell
  • 482
  • 4
  • 17
-1

Thanks everyone who answered my question. Found the answer what i was looking for below and hope it will help others.

http://blog.tech-cats.com/2008/01/using-optional-parameters-in-sql-server.html

melbourne-geek
  • 377
  • 1
  • 11
  • 2
    It is great you have found a solution to your problem. However, it has nothing to do with the question about LINQ you have asked. ;-) – mipe34 Jan 29 '13 at 23:50