Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. SequenceEqual and LINQ

SequenceEqual and LINQ

Scheduled Pinned Locked Moved LINQ
questioncsharplinqhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fadi Yoosuf
    wrote on last edited by
    #1

    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

    N 2 Replies Last reply
    0
    • F Fadi Yoosuf

      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

      N Offline
      N Offline
      Niladri_Biswas
      wrote on last edited by
      #2

      Have you looked at implementing IComparer on Item?

      Niladri Biswas

      1 Reply Last reply
      0
      • F Fadi Yoosuf

        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

        N Offline
        N Offline
        Niladri_Biswas
        wrote on last edited by
        #3

        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

        F 1 Reply Last reply
        0
        • N Niladri_Biswas

          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

          F Offline
          F Offline
          Fadi Yoosuf
          wrote on last edited by
          #4

          Thank you very much

          N 1 Reply Last reply
          0
          • F Fadi Yoosuf

            Thank you very much

            N Offline
            N Offline
            Niladri_Biswas
            wrote on last edited by
            #5

            :)

            Niladri Biswas

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups