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. query condition for a list in an object

query condition for a list in an object

Scheduled Pinned Locked Moved LINQ
questioncsharpdatabaselinqcom
3 Posts 2 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.
  • A Offline
    A Offline
    acroitoriu
    wrote on last edited by
    #1

    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"

    J 1 Reply Last reply
    0
    • A acroitoriu

      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"

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      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;

      A 1 Reply Last reply
      0
      • J J4amieC

        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;

        A Offline
        A Offline
        acroitoriu
        wrote on last edited by
        #3

        Thanks a lot for the suggestion. It solved my problem. Andrei

        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