-2

I have a list of a model called results. I need to get those values from the results list which contain this particular string.

List<Search> results = new List<Search>();

results = db.Users.Select(f => new Search{ Name = f.Name, Type = f.OrganizationType.Name, County = f.County.Name }).ToList();

results = results.Where(w => (model.Name == null || w.Name.Contains(model.Name))).ToList(); 

While the first result query returns 5000 rows, the second one returns 0. What i am trying to do in the second query is if the Name is null or if the Name contains part of a string, add it to the results list. Am I missing something?

I did check a couple of links which basically asked me to do the same like Check if a string within a list contains a specific string with Linq

I have checked the value for model.Name and it shows up properly. Also the query works if there is no search string that is when Model.Name = null, I get all the records

Community
  • 1
  • 1
TheFallenOne
  • 1,598
  • 2
  • 23
  • 57
  • 4
    It's bad practice to fetch all rows into the ram and filter afterwards. Your sql server should already do the job to reduce cpu/network load, overhead and ram usage. – fubo Feb 13 '17 at 14:39
  • 1
    How do we know that the results actually contain `model.Name`? Where is that set? We can't be sure...also, you aren't dealing with casing issues. `Aaron` is not the same as `aaron`. – rory.ap Feb 13 '17 at 14:40
  • Who is `model` field, a `Search` variable? – ocuenca Feb 13 '17 at 14:41
  • @octavioccl Yes it is a search field and it is being populated right – TheFallenOne Feb 13 '17 at 14:43
  • The "first query" does not filter anything, it just take all results from datasource and maps them to Search objects – AFract Feb 13 '17 at 14:43
  • Regarding the second, please be careful to case, and also be sure that model.Name can really be equal to null. I suspect it can be empty string instead, which ruins your search logic. – AFract Feb 13 '17 at 14:45
  • @fubo Is there a case insensitive Contains ? – TheFallenOne Feb 13 '17 at 14:45
  • @The_Outsider sure `"foo".IndexOf("FOO", StringComparison.OrdinalIgnoreCase) > 0` – fubo Feb 13 '17 at 14:48
  • You can call .ToUpper for each side of comparison. String.Equals(row.Name, "test", StringComparison.OrdinalIgnoreCase) is also translated in linq to sql but of course does not behave as Contains() – AFract Feb 13 '17 at 14:52

1 Answers1

1

Consider this statement: Name is null or if the Name contains part of a string I hope you need to check an item in db.Users for null and Contains. One more thing I have to add here is- if x.Name is null then the following .Contains will raise NullReferanceException So you have to consider that as well. Now take a look into the following query:

List<Search> results = db.Users.Where(x=> x.Name==null || (x.Name !=null && x.Name.Contains(model.Name)))
                               .Select(f => new Search{ Name = f.Name, Type = f.OrganizationType.Name, County = f.County.Name }).ToList();
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • `Contains('foo')` is translated into `Like'%foo%'` so there should be no `ToLower()` required because `Like` is already case insensitive – fubo Feb 13 '17 at 14:45
  • While I understand there might be performance issues by doing it in two queries, how is my query different to yours? – TheFallenOne Feb 13 '17 at 14:48
  • @The_Outsider `ToList()` writes the result into your ram. You do it in your application, un-lucky does it in the sql query – fubo Feb 13 '17 at 14:50
  • @fubo So does it remove the case sensitive issue of the contains? – TheFallenOne Feb 13 '17 at 14:52
  • 1
    @fubo: thanks for the new information `Contains('foo') is translated into Like'%foo%'`. Let me remove the `.ToLower()` and update the answer – sujith karivelil Feb 13 '17 at 14:56