0

I have this query

from d in db.v_Report_CompanySearches
orderby d.PersonName ascending
where d.PersonName.ToLower().Contains(mPersonName)
group d.PersonName by d.PersonName into d
select ((v_Report_CompanySearches)d)

but this line is breaking it

group d.PersonName by d.PersonName into d

I'm looking to extract only the column PersonName, and also only the unique values and sorted. But this is crashing. Does anyone know what's wrong?

Thanks

maccettura
  • 10,514
  • 3
  • 28
  • 35
omega
  • 40,311
  • 81
  • 251
  • 474

2 Answers2

0

Change to:

from d in db.v_Report_CompanySearches
   orderby d.PersonName ascending
  where d.PersonName.ToLower().Contains(mPersonName)
  group d by d.PersonName into newGroup
  select ((v_Report_CompanySearches)newGroup)

You want to group the 'd' objects, not the d.PersonName properties. You also need a place for this group to live, hence 'newGroup'. Finally, I would recommend doing your casting outside of the linq statement. That's asking for headache. If you want only unique items (no repeats), call .Distinct on the result of the query. i.e.

<your_query_result>.Distinct();
JWalli
  • 9
  • 2
0

if you just want to sort by taking unique names

var uniqueNames = db.v_Report_CompanySearches
            .Select(x => x.PersonName)  //Only return Names
            .Distinct()     // Make to Unique
            .Where(y => y.ToLower().Contains(mPersonName.ToLower()))  //where 
            .OrderBy(x => x);   // Order BY 
            //.OrderByDescending(x => x); Order By desc 
go..
  • 958
  • 7
  • 15