0

I have a database table with a lot of data. There is one attribute that I would like to extract from it. This attribute is commonly the same for each entry. There are about three variations of it. Is it possible to get those three variations in a list without traversing through the entire database.

For example I don't want to do the following for efficiency:

foreach (var x in table) 
{
    list.Add(x.attribute);
{

I would just like the unique attributes and ignore all redundant ones. I imagine if I just did an if(notSeen()) it would still traverse through the table and not save much work.

Are there any tools to help with this sort of process?

Jon
  • 278
  • 8
  • 23

1 Answers1

1

assuming you're using LINQ to SQL/Entities, for sure it's possible doing that on the DB level. You can do that like this (LINQ to Entities that translates to SQL with DISTINCT / GroupBy statement): table.Select(k=>k.attribute).Distinct();. If there's more than one grouping-key, you can use this syntax also: table.GroupBy(k=>new{k.ItemOne, k.ItemTwo}).Select(k=>k.First());

Hope this help.

Shahar Gvirtz
  • 2,418
  • 1
  • 14
  • 17
  • That was exactly what I was looking for, I guess I was looking for something more complicated. Thanks. – Jon Aug 07 '15 at 15:40
  • Getting the whole table from the database and then calling `Distinct(...)` will still iterate over every item. Linq is just doing it under the covers. If you have access to the database it would likely be faster for you to make a query that returns distinct results. – carpenter Aug 07 '15 at 16:02
  • LINQ translates "Distinct" to SQL statement, and not retrieving the whole table (as long you're not calling AsEnumerable(), ToArray() or something like that before that force retrieving the whole table). – Shahar Gvirtz Aug 07 '15 at 16:09