views:

58

answers:

0
+1  Q: 

Linq subqueries

Hi, I am trying to modify some open source code to change how it filters search results. I want to query by the 'name' of a record instead of it's key. (I have records with duplicate names I want to pull back, but different keys.)

This pulls back all the records

 public static IEnumerable<ticket> GetTickets(stDataContext db, bool? active)
    {
        return from p in db.tickets
               where
                   (active == null ? true :
                   ((bool)active ? p.active : !p.active))
               select p;
    }

The next level up in the code queries against this return by the ID key. I'm not sure which place I should modify.

 public static IEnumerable<ticket> Search(stDataContext db, string[] keywords, int usr, DateTime dtFrom, DateTime dtTo, int prty, int stat, bool onlyOpen, int grp, int subgrp,string subgroupName, bool? active)
    {
        return from p in GetTickets(db, active)
               where
                   p.title.ToLower().Contains(keywords[0].ToLower()) &&
                   p.title.ToLower().Contains(keywords[1].ToLower()) &&
                   p.title.ToLower().Contains(keywords[2].ToLower()) &&
                   p.title.ToLower().Contains(keywords[3].ToLower()) &&
                   p.title.ToLower().Contains(keywords[4].ToLower()) &&
                   p.title.ToLower().Contains(keywords[5].ToLower()) &&
                   p.title.ToLower().Contains(keywords[6].ToLower()) &&
                   p.title.ToLower().Contains(keywords[7].ToLower()) &&
                   p.title.ToLower().Contains(keywords[8].ToLower()) &&
                   p.title.ToLower().Contains(keywords[9].ToLower()) &&

                   p.submitter == (usr < 0 ? p.submitter : usr) &&
                   p.submitted >= dtFrom &&
                   p.submitted <= dtTo &&
                   (prty == 0 ? true : p.priority == prty) &&
                   (stat == 0 ? true : p.ticket_status == stat) &&
                   (!onlyOpen ? true : p.ticket_status != 5) &&
                   (grp == 0 ? true : (
                   (p.sub_unit2.unit_ref == grp) ||
                   (p.sub_unit.unit_ref == grp))) &&
                   (subgrp == 0 ? true : (
                   (p.originating_group == subgrp) ||
                   (p.assigned_to_group == subgrp)))
               select p;
    }

The last 2 lines of the where cause are the ones I want to change. Instead of using the fkey (p.originating_group,p.assigned_to_group, and subgrp) I want to use the name subgroupName.

Should I filter the records by the subgroup in GetTickets method or at the Search method and how to I add a subquery to the linq?

Thanks, Tim