1

how can i compare the items in a string[] array against a generic list that contains objects using LINQ?

this generic list contains objects called picInfo. picinfo class looks like this:

[ProtoContract]
public class PicInfo
{
[ProtoMember(1)]
public string fileName { get; set; }
[ProtoMember(2)]
public string completeFileName { get; set; }
[ProtoMember(3)]
public string filePath { get; set; }
[ProtoMember(4)]
public byte[] hashValue { get; set; }

public PicInfo() { } 
}

the string[] array contains filepaths of pictures. im trying to check if the generic list already contains the path of this particular picture.

the generic list of the pictures looks like this:

List<PicInfo> pi = new List<PicInfo>();

if the generic list already has that picture, i'd like to remove the item from the string[] array.

i can do this using the foreach loop and compare the items 1 by 1. but how can i do this using linq?

thanks in advance!

1 Answer 1

4

You could just determine the duplicates and then remove them:

var duplicates = pi.Where(p => picArray.Contains(p.filePath);
foreach(string fileName in duplicates)
{
   //do something
}

But a cleaner approach and more readable would be using Except() and creating a new array, although this is a little more overhead for the array creation:

picArray = picArray.Except(pi.Select(p=> p.filePath)).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.