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. C#
  4. lambda expression to retrieve sequence based on an array value where the array is stored in the sequence

lambda expression to retrieve sequence based on an array value where the array is stored in the sequence

Scheduled Pinned Locked Moved C#
linqdata-structuresfunctionalhelp
7 Posts 3 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.
  • K Offline
    K Offline
    Krellon
    wrote on last edited by
    #1

    Hi guys, Probably a simple task but i'm struggling with it. I have a list of objects, lets call this a list of Object1. Each Object1 contains a list of another object lets say Object2. Object2 has 2 properties. 'Name' and 'Value'. I want to be able to select Object1 where Object2.Name equals a specified value using lambda. eg

    class Object2
    {
    public string Name {get; set;}
    public string Value {get; set;}
    }

    class Object1
    {
    public List theList = new List(){ new Object2(){Name = "Blah", Value = "Blah"}};
    }

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();

        var myList = Object1.Where(m => m.theList.Name == "Blah") // This is where im confused and would like some clarity of operation
        
    }
    

    }

    I hope I explained myself clearly. Many thanks for your help Br Nigel

    OriginalGriffO 1 Reply Last reply
    0
    • K Krellon

      Hi guys, Probably a simple task but i'm struggling with it. I have a list of objects, lets call this a list of Object1. Each Object1 contains a list of another object lets say Object2. Object2 has 2 properties. 'Name' and 'Value'. I want to be able to select Object1 where Object2.Name equals a specified value using lambda. eg

      class Object2
      {
      public string Name {get; set;}
      public string Value {get; set;}
      }

      class Object1
      {
      public List theList = new List(){ new Object2(){Name = "Blah", Value = "Blah"}};
      }

      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();

          var myList = Object1.Where(m => m.theList.Name == "Blah") // This is where im confused and would like some clarity of operation
          
      }
      

      }

      I hope I explained myself clearly. Many thanks for your help Br Nigel

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      There are a lot of things wrong with that! Object1 is a class, not a collection, so you can;t use Where on it. Name is not a property of the list, it;'s a property of teh objects teh list contains. Try this:

              List myList = new List();
              myList.Add(new Object1());
              myList.Add(new Object1());
              myList.Add(new Object1());
              IEnumerable myResults = myList.Where(m => m.theList.FirstOrDefault(o => o.Name == "Blah") != null);
      

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      K Richard DeemingR 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        There are a lot of things wrong with that! Object1 is a class, not a collection, so you can;t use Where on it. Name is not a property of the list, it;'s a property of teh objects teh list contains. Try this:

                List myList = new List();
                myList.Add(new Object1());
                myList.Add(new Object1());
                myList.Add(new Object1());
                IEnumerable myResults = myList.Where(m => m.theList.FirstOrDefault(o => o.Name == "Blah") != null);
        

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        K Offline
        K Offline
        Krellon
        wrote on last edited by
        #3

        Thanks Member 12616185, Yes, Object1 was meant to be a collection of Object1's. My bad for not proof reading. Thanks for spotting and correcting. Your reply worked a treat. Here's the implementation I used which served my purpose.

        var myResults = Object1.Where(m => m.Object2.Select(o => o.Value == "802.11g") != null);
        

        Many thanks again for the help Br Nigel

        Richard DeemingR 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          There are a lot of things wrong with that! Object1 is a class, not a collection, so you can;t use Where on it. Name is not a property of the list, it;'s a property of teh objects teh list contains. Try this:

                  List myList = new List();
                  myList.Add(new Object1());
                  myList.Add(new Object1());
                  myList.Add(new Object1());
                  IEnumerable myResults = myList.Where(m => m.theList.FirstOrDefault(o => o.Name == "Blah") != null);
          

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Probably simpler to replace the .FirstOrDefault(test) != null with .Any(test):

          IEnumerable<Object1> myResults = myList.Where(m => m.theList.Any(o => o.Name == "Blah"));


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          OriginalGriffO 1 Reply Last reply
          0
          • K Krellon

            Thanks Member 12616185, Yes, Object1 was meant to be a collection of Object1's. My bad for not proof reading. Thanks for spotting and correcting. Your reply worked a treat. Here's the implementation I used which served my purpose.

            var myResults = Object1.Where(m => m.Object2.Select(o => o.Value == "802.11g") != null);
            

            Many thanks again for the help Br Nigel

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Member 12616185 wrote:

            var myResults = Object1.Where(m => m.Object2.Select(o => o.Value == "802.11g") != null);

            That's not going to do what you want. It's just going to return every item from the input sequence, since m.Object2.Select(...) returns an IEnumerable<T> which is never equal to null. Try:

            var myResults = Object1.Where(m => m.Object2.Any(o => o.Value == "802.11g"));


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            K 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Probably simpler to replace the .FirstOrDefault(test) != null with .Any(test):

              IEnumerable<Object1> myResults = myList.Where(m => m.theList.Any(o => o.Name == "Blah"));


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Good idea - it's easier to read.

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Member 12616185 wrote:

                var myResults = Object1.Where(m => m.Object2.Select(o => o.Value == "802.11g") != null);

                That's not going to do what you want. It's just going to return every item from the input sequence, since m.Object2.Select(...) returns an IEnumerable<T> which is never equal to null. Try:

                var myResults = Object1.Where(m => m.Object2.Any(o => o.Value == "802.11g"));


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                K Offline
                K Offline
                Krellon
                wrote on last edited by
                #7

                Hi Homer, I thought the query might return all results in the collection where value == 802.11g and Object2 wasn't equal to null. I have so much to learn :) I did a test run and you were right! all objects came back in the collection. I have since implemented your recommendation and that worked :) Here's my test code for ref.

                using System.Collections.Generic;
                using System.Linq;

                namespace ConsoleApp3
                {
                class Program
                {
                static void Main(string[] args)
                {
                List origCollection = new List()
                {
                new Object1() {ID = 1, theList = new List()
                {
                new Object2() { Name = "Mode", Value = "802.11b" },
                new Object2() { Name = "BW", Value = "20" },
                new Object2() { Name = "Chan", Value = "1" },
                new Object2() { Name = "Tech", Value = "SISO" }
                }
                },
                new Object1() { ID = 2, theList = new List()
                {
                new Object2(){ Name = "Mode", Value = "802.11g" },
                new Object2(){ Name = "BW", Value = "20" },
                new Object2(){ Name = "Chan", Value = "1" },
                new Object2(){ Name = "Tech", Value = "SISO" }
                }
                }
                };

                    var theCollection0 = origCollection.Where(m => m.theList.Select(n => n.Value == "802.11b") != null); // Broken
                
                    var theCollection1 = origCollection.Where(m => m.theList.Any(n => n.Value == "802.11b"));            // Works!
                    var theCollection3 = origCollection.Where(m => m.theList.Any(n => n.Value == "802.11z"));            // Returns Empty as expected.
                }
                

                };

                class Object1
                {
                public int ID { get; set; }
                public List theList;
                }

                class Object2
                {
                public string Name { get; set; }
                public string Value { get; set; }
                }
                }

                Many thanks for spotting and fixing this! your a star :) Br Nigel

                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