Question

I am working on an existing online library of MP3 files. One part of the code looks for files in the database that contain a certain string in the series title.

public ICollection<Series> ListTruthAndLifeSeries() {
return (from f in ActiveRecordLinq.AsQueryable<File>()
     where f.SeriesTitle.Contains("Truth and Life")
     orderby f.SeriesTitle
     select new { Name = f.SeriesTitle, Slug = f.SeriesTitleSlug })
                    .ToList().Distinct()
                    .Select(u => Parse(u.Name, u.Slug))
                    .OrderByDescending(u => u.Year)
                    .ThenBy(u => u.Name)
                    .ToList();
    }

This works just fine. But I need to add some sort of condition where it will also return the records whose series title contains "Truth & Life". I attempted to do this by simply adding

or f.SeriesTitle.Contains("Truth & Life")

after the where clause but then Visual Studio puts a bunch of red squiggly lines in and greys out the or statement and everything after it in that method. What is the correct syntax to do this? Changing the data so that none of them use & is not a practical option.

Was it helpful?

Solution

I'm not sure about your actual problem, but one way to solve it (and allow your query to be extensible) is to use any collection derived from IEnumerable<String> like:

List<string> Titles = new List<string>();
Titles.Add("Truth and Life");
Titles.Add("Truth & Life");

 return (from f in ActiveRecordLinq.AsQueryable<File>()
         where Titles.Contains(f.SeriesTitle)
         orderby f.SeriesTitle
         select new { Name = f.SeriesTitle, Slug = f.SeriesTitleSlug })
       .ToList().Distinct()
       .Select(u => Parse(u.Name, u.Slug))
       .OrderByDescending(u => u.Year)
       .ThenBy(u => u.Name)
       .ToList();

Update

C# does not use or it uses || to declare the OR operator.

where f.SeriesTitle.Contains("Truth and Life")
      || f.SeriesTitle.Contains("Truth & Life")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top