-1

I have a string[] of unique words and an IQueryable<> of a unique class, which contains a string. I'd like to remove all items from the IQueryable<> whose string variable does not contain one of the unique words. How can I do this in a way that will not be a serious drain on my code at runtime?

One of the variables is string Name. I have an IQueryable<Project> and a string[] words. I'd like to create a new IQueryable<Project> where all of the Project's Name contains at least one of the words in the words array.

I don't know how exactly how to do this without looping through the IQueryable twice (once to mark the Projects that have a Name containing a word in the array, and again to move those Projects into a new IQueryable) which for a large IQueryable would be a serious time strain.

Servy
  • 202,030
  • 26
  • 332
  • 449
B. Fitzgerald
  • 135
  • 1
  • 16

1 Answers1

1

if the Name property is just a string with multiple words:

var newQueryable = queryable
       .AsEnumerable()
       .Where(project => words
                .Intersect(project.Name.Split(' '), StringComparer.OrdinalIgnoreCase)
                .Any())
       .AsQueryable();

dont use the split function if the Name is already an array which consits of all the words.

H H
  • 263,252
  • 30
  • 330
  • 514
igorc
  • 2,024
  • 2
  • 17
  • 29
  • It's a start but Project.Name has multiple Words – H H Jun 26 '17 at 13:41
  • I tried this exactly and my code threw the exception `{"LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable\`1[System.String] Intersect[String](System.Collections.Generic.IEnumerable\`1[System.String], System.Collections.Generic.IEnumerable\`1[System.String], System.Collections.Generic.IEqualityComparer\`1[System.String])' method, and this method cannot be translated into a store expression."}` – B. Fitzgerald Jun 26 '17 at 14:35
  • @jαsοndιnAlt This works great. Do you know what I could add to make it ignore punctuation in the project.Name string? – B. Fitzgerald Jun 26 '17 at 15:03
  • you will have to implement a custom IgnorePunctuationEqualityComparer and replace "StringComparer.OrdinalIgnoreCase" with it. – igorc Jun 26 '17 at 15:15
  • You can accomplish the ignoring by tweaking `string.Split()`. – H H Jun 26 '17 at 17:05