none
LINQ query RRS feed

  • Question

  • Hi guys,

    I have a class as

    Class student

    {

      public string Name{ get; set; }

      public int Age{ get; set; }

      ......

     public List<string> Books{ get; set; }

    Is there any LINQ syntax to query all books the students own?

    Thanks.

    Best regards,

    Richard


    lurc

    Thursday, December 28, 2017 3:17 PM

Answers

  • I don't understand why you need a LINQ query at all, assuming the Books property IS the set of books that the student owns? In which case, you just access it directly.

    var student = new student();
    var ownedBooks = student.Books;

    If you want to loop around that set of books (perhaps with some condition) then you can use foreach with a bit of LINQ, e.g:

    foreach (var book in ownedBooks.Where((b) => b.StartsWith("A")))
    {
       Console.WriteLine(book);
    }

    Edit: Realised you may be talking about collection of books across a whole set of students.

    Assuming the list isn't ridiculously long, this approach simply concatenates all books into one long list then 'distincts' them:

    var studentA = new student() {Books = new List<string>() {"Book1", "Book2"}};
    var studentB = new student() { Books = new List<string>() { "Book2", "Book3" } };
    var students = new List<student>() {studentA, studentB };
    
    var allBooks = new List<string>();
    foreach (var student in students)
    {
        allBooks.AddRange(student.Books);
    }
    
    var allDistinctBooks = allBooks.Distinct();



    • Edited by RJP1973 Thursday, December 28, 2017 4:14 PM
    • Marked as answer by richardlu Friday, December 29, 2017 5:42 AM
    Thursday, December 28, 2017 3:25 PM

All replies

  • I don't understand why you need a LINQ query at all, assuming the Books property IS the set of books that the student owns? In which case, you just access it directly.

    var student = new student();
    var ownedBooks = student.Books;

    If you want to loop around that set of books (perhaps with some condition) then you can use foreach with a bit of LINQ, e.g:

    foreach (var book in ownedBooks.Where((b) => b.StartsWith("A")))
    {
       Console.WriteLine(book);
    }

    Edit: Realised you may be talking about collection of books across a whole set of students.

    Assuming the list isn't ridiculously long, this approach simply concatenates all books into one long list then 'distincts' them:

    var studentA = new student() {Books = new List<string>() {"Book1", "Book2"}};
    var studentB = new student() { Books = new List<string>() { "Book2", "Book3" } };
    var students = new List<student>() {studentA, studentB };
    
    var allBooks = new List<string>();
    foreach (var student in students)
    {
        allBooks.AddRange(student.Books);
    }
    
    var allDistinctBooks = allBooks.Distinct();



    • Edited by RJP1973 Thursday, December 28, 2017 4:14 PM
    • Marked as answer by richardlu Friday, December 29, 2017 5:42 AM
    Thursday, December 28, 2017 3:25 PM
  • Try this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApp6
    {
        class Program
        {
            static List<Student> mStudents;
            static void Main(string[] args)
            {
                var lastName = "Adams";
                PopulateList();
    
                var results = mStudents.FirstOrDefault(s => s.LastName == lastName && s.Books != null);
                if (results != null)
                {
                    Console.WriteLine($"Books for {lastName}");
                    results.Books.ForEach(b => Console.WriteLine(b.Name));
                }
    
                lastName = "Smith";
                results = mStudents.FirstOrDefault(s => s.LastName == lastName && s.Books != null);
                if (results != null)
                {
                    results.Books.ForEach(b => Console.WriteLine(b.Name));
                }
                else
                {
                    Console.WriteLine($"No books for {lastName}");
                }
    
                Console.ReadLine();
            }
            static void PopulateList()
            {
                mStudents = new List<Student>()
                {
                    new Student()
                    {
                        LastName = "Payne",
                        Books = new List<Book>()
                        {
                            new Book() { id = 1, Name = "ABC" },
                            new Book() { id = 3, Name = "DEF" }
                        } },
                    new Student()
                    {
                        LastName = "Adams",
                        Books = new List<Book>()
                        {
                            new Book() { id = 12, Name = "FFF" },
                            new Book() { id = 400, Name = "GGG" }
                        } },
                    new Student() { LastName = "Smith"  }
                };
            }
        }
        public class Student
        {
            public string LastName { get; set; }
            public List<Book> Books { get; set; }
        }
        public class Book
        {
            public int id { get; set; }
            public string Name { get; set; }
        }
    
    }
    
    
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 28, 2017 4:04 PM
    Moderator
  • is the Entity Framework involved in this?
    Thursday, December 28, 2017 6:29 PM