U
U
User992018-03-30 22:01:12
ASP.NET
User99, 2018-03-30 22:01:12

How to select unique value from search results?

Help translate SQL query to LINQ for ASP.NET MVC

select DISTINCT(KNIGI),FIO from Library
where FIO='Антонов Антон Антонович'

That is, I am looking for a person by full name and displaying only unique, non-repeating values.
One person could take the same book several times. If Antonov took mathematics 10 times, physics 5 times, algebra 1 time, I need to print: Mathematics, Physics, Algebra. i.e. only one at a time
AND recommend a book on LINQ

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
eRKa, 2018-03-30
@User99

db.Library.Where(x => x.FIO == "Antonov Anton Antonovich").GroupBy(x => x.KNIGI).Select(x => x.First())

U
User99, 2018-03-30
@User99

var j = (from Library in db.Library
where
  Library.FIO== Антонов Антон Антонович
select new {
  Library.KNIGI,
  Library.FIO
}).Distinct() 
return View(j.ToList());

throws an error on display
The model element passed to the dictionary is of type "System.Collections.Generic.List`1[<>f__AnonymousType1`2[System.String,System.String]]"
and I used IEnumerable

M
MrDywar Pichugin, 2018-03-30
@Dywar

1) https://metanit.com/sharp/tutorial/ Chapter19
Example with extension method only, I like them better
https://dotnetfiddle.net/bFzudC

using System;
using System.Linq;
using System.Collections.Generic;
          
public class Program
{
  public static void Main()
  {
    var ivan = new User() { Id = 1, Name = "Ivan" };
    var vasya = new User() { Id = 2, Name = "Vasya" };
    
    var book1 = new Book() { Id = 1, Name = "1" };
    var book2 = new Book() { Id = 2, Name = "2" };
    var book3 = new Book() { Id = 3, Name = "3" };
    var book4 = new Book() { Id = 4, Name = "4" };
    
    book1.Users.Add(ivan);
    book1.Users.Add(ivan);		
    book1.Users.Add(vasya);
    book1.Users.Add(vasya);
    
    book2.Users.Add(ivan);
    book2.Users.Add(ivan);		
    book2.Users.Add(vasya);
    book2.Users.Add(vasya);
    
    
    book4.Users.Add(ivan);
    
    var booksRepository = new List<Book>();
    booksRepository.Add(book1);
    booksRepository.Add(book2);
    booksRepository.Add(book3);
    booksRepository.Add(book4);
    
    
    var result = booksRepository.Where(b => b.Users.Any(u => u.Name == "Ivan")).ToList();
    foreach (Book book in result)
    {
      Console.WriteLine(book.Name);
    }
  }
  
  public class User
  {
    public long Id {get;set;}
    public string Name {get;set;}
    
    // List<Book> ...
  }
    
  public class Book
  {
    public long Id {get;set;}
    public string Name {get;set;}
    
    public List<User> Users {get;set;}
    
    public Book(){
      Users = new List<User>();
    }
  }
}

And as I understand it, Books and Users have many-to-many relationships, i.e. where it is easier to take from there and you need to make a request.
It would be easier to go from User, but according to your "Library" table, I understand that this is not so.
Perhaps the record comes with a date, they say it took 10 times and the date, the conditions may still be what, according to this table, the search is in progress, it took at least 1 time. "Any", "Contains", etc. deal with it well.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question