Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

using linq to get unique values

using linq to get unique values

using linq to get unique values


I'm a complete newbie to linq.

I'm trying to figure out how to get an array of objects out of another array of objects filtering by distinct fields.

The source array consists of instances of a clsFileRecord class. It has these fields:

bool Exists;
string FolderPath;
string FileName;
string FileSize;

I want to filter the source array by FolderPath. I want to grab the instances of the clsFileRecord for which the FolderPath is unique. In other words, as I iterate through the source array, if I haven't yet encountered a clsFileRecord whose Folder path is "x/y/z", then added to the destination array. If I have already encountered a clsFileRecord whose Folder path is "x/y/z", ignore it.

In the end, each clsFileRecord in the destination array should have a unique FolderPath. I'm not worried about different FileNames as that field is going to be ignored in the destination array.

Can someone please help me work out a linq statement that will accomplish this? Thank you very much.

RE: using linq to get unique values

Do you know SQL? You use Group By there to get distinct values.
This might help you


List<clsFileRecord> distinctFolders = allFiles
  .GroupBy(f => f.FolderPath)
  .Select(p => p.First())

This is making one thing easier than doing that with SQL: To pick a first full object/array item. In SQL you can't combine * (all fields, full object) with Group By, you have to define aggregations on the fields you don't group by.

As you only need the folder path, you don't need to select full File objects, you might create a list of only file paths. By the way I suggest you rather work with Lists than arrays, both implement IEnumarable, but Lists are more versatile, generic (strongly typed) collections are even better. If you insist on arrays you can of course also use ToArray(), don't forget to declare the variable for the result as array, then.

So as you only want the paths, you can limit yourself to only select that single class field:


List<String> distinctFolders = allFiles
  .GroupBy(f => f.FolderPath)
  .Select(p => p.First().FolderPath)

Untested. You might need to fiddle a bit with this, but it should give you an idea.

Bye, Olaf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close