Reputation: 1237
So, I'm basically creating a version control-like system. I have the following class Form
:
public class Form
{
public long Id {get; private set;}
/* among other things */
}
Then, another class like so:
public class Conflict
{
List<Form> localForms;
List<Form> remoteForms;
/* among other things */
}
and then a main class that maintains a List
of Conflict
s.
public class Main
{
List<Conflict> conflicts;
/*
* more stuff...
*/
public void AddFormConflict(List<Form> locals, List<Form> remotes)
{
...what goes here?
}
}
I want to make sure that when a new Conflict
object is about to be added, it doesn't contain duplicate data; in other words, I want to compare Id
s of the List<Form> locals
argument with Id
s contained by the localForms
member of the conflicts
list. And same for the remotes as well. Furthermore, I not only want to find out whether or not such a match exists, but I also want to get a reference to it.
Basically long story short, I want to compare the properties in a list of objects with corresponding properties in another similarly-structured list of objects... which are contained by another class which is in a list.
I'm pretty sure there's gotta be some relatively simple way to do something like this with linq in about 2-3 lines, right? I just can't for the life of me wrap my head around all the layers! Ugh. Help please?
Upvotes: 1
Views: 85
Reputation: 10068
Making the properties public
public class Conflict
{
public List<Form> localForms { get; set; }
public List<Form> remoteForms { get; set; }
/* among other things */
}
You can check for duplicates like this
public class Main
{
List<Conflict> conflicts;
public void AddFormConflict(List<Form> locals, List<Form> remotes)
{
if (conflicts.Any(c => c.localForms.Any(lf => locals.Any(lc => lf.Id == lc.Id))))
{
//duplicate found for localForms
}
//similarly check for remoteForms
}
}
Upvotes: 1