M
M
Mikhail2018-03-03 21:51:05
C++ / C#
Mikhail, 2018-03-03 21:51:05

How to select data correctly with LINQ?

Hello. There is a structure like this:

class Book
{
   public string Name { get; set; }
   public string Author { get; set; }
}

class User
{
   public string Name { get; set; }
   public int Age { get; set; }
   public Book[] Books;
}

I'm trying to choose the names of all the people who have at least one Dostoevsky book like this:
var us = Users.SelectMany(u => u.Books)
       .Where(b => b.Author == "Достоевский")
       .Distinct()
       .Select(b => b.Name);

But I only get book names. How to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Yudakov, 2018-03-03
@mak_ufo

Users
    .Where(u => u.Books
    .Any(b => b.Author == "Достоевский"))
    .Select(u => u.Name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question