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. Find missing elements

Find missing elements

Scheduled Pinned Locked Moved LINQ
databasetutorialquestion
3 Posts 3 Posters 4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I have a collection of objects that all of a property Index. I am doing some stuff to grab certains ones and combine them. Now I would like to find any gaps. So I have say 4 elements in a collection with indecies 3,4,5,8,9 since 6 and 7 are missing I would like to know that and then add them from a different collection. Any ideas? I was thinking if I could find the 5 I could cycle up to 8. So to do this I can find the max (9) and say if the item is not the max index but it is missing a + 1. Not sure how to get that into a proper query though :sigh:

    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

    K F 2 Replies Last reply
    0
    • L Lost User

      I have a collection of objects that all of a property Index. I am doing some stuff to grab certains ones and combine them. Now I would like to find any gaps. So I have say 4 elements in a collection with indecies 3,4,5,8,9 since 6 and 7 are missing I would like to know that and then add them from a different collection. Any ideas? I was thinking if I could find the 5 I could cycle up to 8. So to do this I can find the max (9) and say if the item is not the max index but it is missing a + 1. Not sure how to get that into a proper query though :sigh:

      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

      K Offline
      K Offline
      Kubajzz
      wrote on last edited by
      #2

      I doubt there is a reasonable way to find those items with a Linq query... I think a simple for loop would do the best job.

      1 Reply Last reply
      0
      • L Lost User

        I have a collection of objects that all of a property Index. I am doing some stuff to grab certains ones and combine them. Now I would like to find any gaps. So I have say 4 elements in a collection with indecies 3,4,5,8,9 since 6 and 7 are missing I would like to know that and then add them from a different collection. Any ideas? I was thinking if I could find the 5 I could cycle up to 8. So to do this I can find the max (9) and say if the item is not the max index but it is missing a + 1. Not sure how to get that into a proper query though :sigh:

        Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

        F Offline
        F Offline
        Fernando Soto
        wrote on last edited by
        #3

        Hi Collin; This can be done with linq as shown in the code snippet below.

        // Your values from Index property
        List<int> myElements = new List<int>() { 8, 5, 9, 3, 4 };

        // Create all integers from min value to max value in above collection
        var range = from n in Enumerable.Range(myElements.Min(), myElements.Max() - myElements.Min() + 1)
        select n;

        // Find all missing values
        List<int> result = range.Except(myElements).ToList();

        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