views:

673

answers:

3

I have a LINQ to ENTITY query that pulls from a table, but I need to be able to create a "fuzzy" type search. So I need to add a where clause that searches by lastname IF they add the criteria in the search box (Textbox, CAN be blank --- in which case it pulls EVERYTHING).

Here is what I have so far:

    var query = from mem in context.Member
                orderby mem.LastName, mem.FirstName
                select new
                {
                    FirstName = mem.FirstName,
                    LastName = mem.LastName,

                };

That will pull everything out of the Member table that is in the Entity object.

Then I have an addition to the logic:

sLastName = formCollection["FuzzyLastName"].ToString();

if (!String.IsNullOrEmpty(sLastName))
   query = query.Where(ln => ln.LastName.Contains(sLastName));

The problem is when the search button is pressed, nothing is returned (0 results). I have run the query against the SQL Server that I expect to happen here and it returns 6 results.

This is the query I expect:

SELECT mem.LastName, mem.FirstName FROM Members mem WHERE mem.LastName LIKE '%xxx%'

(when xxx is entered into the textbox)

Anyone see anything wrong with this?

EDIT: Fixed the SELECT query. I meant for it to read LIKE '%xxx%' (NOT = 'xxx")

+2  A: 

I think you want to use the Contains() function of the string parameter like this:

var query = from mem in context.Member
    where mem.LastName.Contains("xxx")
    orderby mem.LastName, mem.FirstName
    select new
    {
        FirstName = mem.FirstName,
        LastName = mem.LastName,
    };

I think you can also use StartsWith() and EndsWith() which would be equivalent to the SQL 'xxx%' and '%xxx" respectively.

SkippyFire
But what about when it is blank? If the .Contains() is there and the field is empty, will will return nothing?.The reason I was adding the .Where clause after was in the case that nothing is entered (so I dont have to have a separate query for both)
SlackerCoder
You can do it all as one query, just make sure that the string isn't empty before running the query. I think you will get all the records if the string is empty.
SkippyFire
I tested the theory of all records on an empty string to get 0 results. If that would have been the solution, I would have been happy, but alas, still no solution to this issue...
SlackerCoder
+1  A: 
SELECT mem.LastName, mem.FirstName FROM Members mem WHERE mem.LastName = 'xxx'

That means that you want the last name to be equal to 'xxx'. What you write in your above post is that the lastname should contain 'xxx'.

To get it to equal you should write:

if (!String.IsNullOrEmpty(sLastName))
   query = query.Where(ln => ln.LastName == sLastName);

Perhaps you should look at ignore case:

if (!String.IsNullOrEmpty(sLastName))
   query = query.Where(ln => ln.LastName.Equals(sLastName, StringComparison.InvariantCultureIgnoreCase));
Oskar Kjellin
Sorry I fixed the post, thanks for pointing that out. I have the == query working just fine, the users just need a way to make a "fuzzy" search for names.
SlackerCoder
+1  A: 

Add your "select new" to the query only after you append your "Where" clause.

Hence append your select clause using object call syntax as you did with the where clause.

Untested, please excuse small errors, but the general concept would be....

   using( someContent sc = new someContent())
   {
      var query = sc.Member.OrderBy( i => i.LastName)
                    .ThenBy( i => i.FirstName);

      sLastName = formCollection["FuzzyLastName"].ToString();

      if (!String.IsNullOrEmpty(sLastName))
          query = query.Where(ln => ln.LastName.Contains(sLastName));

      query = query.Select( i => new
                {
                    FirstName = i.FirstName,
                    LastName = i.LastName,

                });
    }
kervin