Answer the question
In order to leave comments, you need to log in
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='Антонов Антон Антонович'
Answer the question
In order to leave comments, you need to log in
db.Library.Where(x => x.FIO == "Antonov Anton Antonovich").GroupBy(x => x.KNIGI).Select(x => x.First())
var j = (from Library in db.Library
where
Library.FIO== Антонов Антон Антонович
select new {
Library.KNIGI,
Library.FIO
}).Distinct()
return View(j.ToList());
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>();
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question