SequenceEqual and LINQ
-
Hi I have a class named Item. It has the public members, 'Name' and 'Subitems'. I have declared a list of Items (List itemList). Now I want to write a function which accepts a new 'Item' and decides if itemList already has the 'Item'. I tried writing a function like bool Check(Item p) { return itemList.Exists(m=> m.Name == p.Name && m.SubItems.SequenceEqual(p.SubItems)); } Here the issue is, default comparing is done for SequenceEqual method. I want to compare SubItems using the Name property of the SubItems. How can I modify my function so that SequenceEqual works based on the NameProperty of the subitem? Thank you Fadi
-
Hi I have a class named Item. It has the public members, 'Name' and 'Subitems'. I have declared a list of Items (List itemList). Now I want to write a function which accepts a new 'Item' and decides if itemList already has the 'Item'. I tried writing a function like bool Check(Item p) { return itemList.Exists(m=> m.Name == p.Name && m.SubItems.SequenceEqual(p.SubItems)); } Here the issue is, default comparing is done for SequenceEqual method. I want to compare SubItems using the Name property of the SubItems. How can I modify my function so that SequenceEqual works based on the NameProperty of the subitem? Thank you Fadi
Have you looked at implementing IComparer on Item?
Niladri Biswas
-
Hi I have a class named Item. It has the public members, 'Name' and 'Subitems'. I have declared a list of Items (List itemList). Now I want to write a function which accepts a new 'Item' and decides if itemList already has the 'Item'. I tried writing a function like bool Check(Item p) { return itemList.Exists(m=> m.Name == p.Name && m.SubItems.SequenceEqual(p.SubItems)); } Here the issue is, default comparing is done for SequenceEqual method. I want to compare SubItems using the Name property of the SubItems. How can I modify my function so that SequenceEqual works based on the NameProperty of the subitem? Thank you Fadi
Let's start by making a sequence of items that match by name:
var nameMatches = from item in itemList where item.Name == p.Name select item;
We need to compare those items against the sequence of names in p's subitems. Lets get the sequence by this query:
var pnames = from subitem in p.SubItems select subitem.Name;
Now you want to find all the elements from nameMatches where the sequence of names matches. How are you going to get the sequence of names? Well, we just saw how to do that with pnames, so do the same thing:
var matches = from item in nameMatches
let subitemNames =
(from subitem in item.SubItems select subitem.Name)
where pnames.SequenceEquals(subitemNames)
select item;And now you want to know, are there any matches? return matches.Any(); That should work just fine as is. But if you want to be really buff you can write the whole thing in one big query!
return (
from item in itemList
let pnames =
(from psubitem in p.SubItems select psubitem.Name)
let subitemNames =
(from subitem in item.SubItems select subitem.Name)
where item.Name == p.Name
where pnames.SequenceEquals(subitemNames)
select item).Any();Hope it helps :) Vote me
Niladri Biswas
modified on Wednesday, July 1, 2009 12:02 PM
-
Let's start by making a sequence of items that match by name:
var nameMatches = from item in itemList where item.Name == p.Name select item;
We need to compare those items against the sequence of names in p's subitems. Lets get the sequence by this query:
var pnames = from subitem in p.SubItems select subitem.Name;
Now you want to find all the elements from nameMatches where the sequence of names matches. How are you going to get the sequence of names? Well, we just saw how to do that with pnames, so do the same thing:
var matches = from item in nameMatches
let subitemNames =
(from subitem in item.SubItems select subitem.Name)
where pnames.SequenceEquals(subitemNames)
select item;And now you want to know, are there any matches? return matches.Any(); That should work just fine as is. But if you want to be really buff you can write the whole thing in one big query!
return (
from item in itemList
let pnames =
(from psubitem in p.SubItems select psubitem.Name)
let subitemNames =
(from subitem in item.SubItems select subitem.Name)
where item.Name == p.Name
where pnames.SequenceEquals(subitemNames)
select item).Any();Hope it helps :) Vote me
Niladri Biswas
modified on Wednesday, July 1, 2009 12:02 PM
Thank you very much
-
Thank you very much
:)
Niladri Biswas