-3

My intentions are to aggregate the results instead of narrow them down.

if (Request.QueryString["VenueType"] == null)
    Renders = _renderContext.Renders;
else
{
    List<int> venueTypeIds = Request.QueryString["VenueType"].Split(',')
        .Select(int.Parse).ToList();
    Renders = _renderContext.Renders.Where(v => venueTypeIds.Contains(v.VenueTypeId));
}

// SECOND CRITERION:
if (Request.QueryString["SearchTerm"] != null)
    Renders = Renders.Where(r => r.Title.ToLower()
        .Contains(Request.QueryString["SearchTerm"].ToLower()));

// ADDITIONAL CRITERION:
if (Request.QueryString["EventType"] != null)
{
    List<int> eventTypeIds = Request.QueryString["EventType"].Split(',')
        .Select(int.Parse).ToList();
    Renders = Renders.Where(w => eventTypeIds.Contains(w.EventTypeId));
}

if (Request.QueryString["DisplayFormat"] != null)
{
    List<int> displayFormatIds = Request.QueryString["DisplayFormat"].Split(',')
        .Select(int.Parse).ToList();
    Renders = Renders.Where(w => displayFormatIds.Contains(w.DisplayFormatId));
}

The Query Strings are coming from groups of checkboxes which may have one to many options. The results I am trying to return should not narrow down after the first set of criteria but instead return more results.

Question: How do I use either a where clause or GroupBy clause to get an aggregate of returned items rather than to narrow my returned items?

Eric Bishard
  • 5,201
  • 7
  • 51
  • 75
  • I'm voting this answer to be closed because even though I have stated the question and I have an answer that helped me. It may have a slight syntax error but it helped me. Others have downvoted it and if others think it's bad it must be bad even though it immensely helped me. I will not be posting as much on Stack Overflow and will instead look to my peers for answers to questions I have questions about. Thanks for putting me in my place Stack Overflow! – Eric Bishard Oct 29 '14 at 12:39

1 Answers1

-1

If I understand your question correctly you could build your results gradually, something like this (code reduced for brevity)...

IQueryable query = from x in Context.Stuff
                   select x;

List<Stuff> output = List<Stuff>();

output = query.Where(r => r.Title).Contains("SearchTerm")).ToList();
output += query.Where(r => r.Title).Contains("DifferentSearchTerm")).ToList();

See this So post for how to perform an explicit Group By in Linq, although in my humble opinion given the complexity of your criteria it would be fairly unmaintainable.

Community
  • 1
  • 1
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • 1
    @EricB Your welcome, the key here is to keep the initial IQueryable intact, and to retrieve results and effectively aggregate them yourself into an 'output' variable... and don't be put off by the somewhat pedantic element that can prevail on SO. – Paul Zahra Oct 27 '14 at 14:56
  • 1
    And heres linq with having and groupby... euch http://stackoverflow.com/questions/18050386/linq-to-sql-having-and-group-by – Paul Zahra Oct 27 '14 at 15:00