I had a question about how to get list objects by searching for their field names using LINQ. I encoded simple classes Libraryand Bookfor this:
class Book
{
public string title { get; private set; }
public string author { get; private set; }
public DateTime indexdate { get; private set; }
public int page { get; private set; }
public Book(string title,string author, int page)
{
this.title = title;
this.author = author;
this.page = page;
this.indexdate = DateTime.Now;
}
}
class Library
{
List<Book> books = new List<Book>();
public void Add(Book book)
{
books.Add(book);
}
public Book GetBookByAuthor(string search)
{
}
}
So, I want to get instances Bookfor which certain fields are equal to certain rows, for example
if(Book[i].Author == "George R.R. Martin") return Book[i];
I know this is possible with simple loop codes, but I want to do it with LINQ. Is there any way to achieve this?
source
share