lambda expression to retrieve sequence based on an array value where the array is stored in the sequence
-
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
-
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
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!
-
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!
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
-
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!
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
-
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
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 anIEnumerable<T>
which is never equal tonull
. 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
-
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
Good idea - it's easier to read.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
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 anIEnumerable<T>
which is never equal tonull
. 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
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