query condition for a list in an object
-
Hi all, I just started working with Linq and I need some help with one query. I have a class that looks (simplified) like the following: using System.Collections.Generic; public class Book { public string Name { get; set; } public string Author { get; set; } public int ID { get; set; } public IList Contents; } public class Chapters { public string Topic { get; set; } } I need to perform a query to get only certain books from a collection that looks like: Book[] books = { new Book { Name = "test", Author="Andrei", ID=1, Contents = new List {new Chapters {Topic = "Chapter 1"}, new Chapters {Topic = "Chapter 2"}} }, new Book { Name = "test anca", Author="Anca", ID=2, Contents = new List {new Chapters {Topic = "Chapter 3"}, new Chapters {Topic = "Chapter 4"}} }, new Book { Name = "test andrei", Author="Andrei", ID=3, Contents = new List {new Chapters {Topic = "Chapter 5"}, new Chapters {Topic = "Chapter 6"}} } }; I am able to write a query to filter my book based on Name, Author or ID: var b = from book in books where book.Name.Contains("Andrei") select book; but, how can I add a condition on the chapter topic? I would like to have a query that will do: from book in books where book.Name.Contains("andrei") && book.Contents.Toipic.Contains("something") select book; How can I add a condition on the list property? Thank you in advance! Andrei
------------ Croitoriu Andrei andrei.croitoriu@gmail.com http://www.stud.usv.ro/~acroitoriu http://spaces.msn.com/acroitoriu/ "No complexity beyond what is necessary"
-
Hi all, I just started working with Linq and I need some help with one query. I have a class that looks (simplified) like the following: using System.Collections.Generic; public class Book { public string Name { get; set; } public string Author { get; set; } public int ID { get; set; } public IList Contents; } public class Chapters { public string Topic { get; set; } } I need to perform a query to get only certain books from a collection that looks like: Book[] books = { new Book { Name = "test", Author="Andrei", ID=1, Contents = new List {new Chapters {Topic = "Chapter 1"}, new Chapters {Topic = "Chapter 2"}} }, new Book { Name = "test anca", Author="Anca", ID=2, Contents = new List {new Chapters {Topic = "Chapter 3"}, new Chapters {Topic = "Chapter 4"}} }, new Book { Name = "test andrei", Author="Andrei", ID=3, Contents = new List {new Chapters {Topic = "Chapter 5"}, new Chapters {Topic = "Chapter 6"}} } }; I am able to write a query to filter my book based on Name, Author or ID: var b = from book in books where book.Name.Contains("Andrei") select book; but, how can I add a condition on the chapter topic? I would like to have a query that will do: from book in books where book.Name.Contains("andrei") && book.Contents.Toipic.Contains("something") select book; How can I add a condition on the list property? Thank you in advance! Andrei
------------ Croitoriu Andrei andrei.croitoriu@gmail.com http://www.stud.usv.ro/~acroitoriu http://spaces.msn.com/acroitoriu/ "No complexity beyond what is necessary"
-
Try some variations of this:
var results = from book in books
where book.Name.Contains("andrei")
&& book.Contents.FirstOrDefault(c => c.Topic.Contains("Chapter 5")) != null
select book;Thanks a lot for the suggestion. It solved my problem. Andrei