0

Ok, this one has me stumped.

I have a collection of objects called Interviews. An Interview has a collection of Notes in it. A Note has string (nvarchar(max) on the database) property called NoteText.

I have a List called keywords.

What I need to do is find all interviews that have a Note that has any of the keywords within its NoteText property.

I have this so far:

var interviewQuery =
from i in dbContext.Interviews  //dbContext was created with Telerik OpenAccess
    .Include(n => n.Notes)
    .Where(i => i.Notes.Any(n => keywords.Contains(n.NoteText) ))
orderby i.WhenCreated descending
select i;

I don't get an error, I just don't get any results either.

davecove
  • 1,001
  • 4
  • 16
  • 36

2 Answers2

0

I'm pretty poor at linq, but this can be easily done with a loop instead.

var matchinginterviews = new List<Interview>();
foreach (var inter in MyInterviewEnumerable)
{
    foreach (var note in inter.NoteCollection)
    {
        foreach (string keyword in keywordList)
        {
            if (note.NoteText.IndexOf(keyword) != -1)
            {
                matchinginterviews.Add(inter);
            }
        }
    }
}
weirdev
  • 1,388
  • 8
  • 20
0

What's causing the empty results is that you're looking for any keyword values that contain the entire content of any of the notes.

We made an extension method ContainsAny:

public static bool ContainsAny(this string s, IEnumerable<string> possibleContained)
{
    foreach (string p in possibleContained)
    {
        if (s == p) return true;
        if (s == null) continue;
        if (s.Contains(p)) return true;
    }

    return false;
}

Then you could do something similar to where you started:

var results = dbContext.Interviews.Where(i => i.Notes.Any(n => n.NoteText.ContainsAny(keywords)));
  • Love the idea... but all that gets me is : `[MainWindow.xaml.cs:MoveNext() line 1648] Execution of 'AVFusion.ExtensionMethods:ContainsAny(String,IEnumerable 1) on the database server side currently not implemented.` – davecove Aug 07 '15 at 16:41