Home > Mobile >  How to show only elements of list that have one Boolean Value set to False? in c#
How to show only elements of list that have one Boolean Value set to False? in c#

Time:06-05

I'm quite new to c# so sorry for kinda "noobish" question.

My main goal is to show only unavalible books when I recall the method that handles giving back the books.

I have a list of items (books) that have values like this:

Books.cs

 public string author, title;
        public bool avalible; 

And also i have a class Audiobooks that inherits from class books, like this:

Audiobooks.cs

public class audiobook : book
{
    public string whoReads;
    public audiobook()
    {

        Console.WriteLine("Set a name of  lector: ");
        this.whoReads = Convert.ToString(Console.ReadLine());

Now in the main class where i handle borrowing/giving back/adding and Displaying all books I want to display only unavalible books when I recall the method that handles giving back the books so the user won't choose the book that is already avalible. Then I will just set the bool avalible to true.

I tried if(books.Contains(false)); ~there was a problem with converting (CS1503) I tried something with foreach but this din't worked too. I will be gratefull for all responses. I'm sorry for any gramaticall errors - english is my 2nd language.

CodePudding user response:

        List<Books> li; // all Books List
        List<Books> li_2;
        var q = from l in li
                where l.available == false
                select l;

        li_2 = q.toList();

CodePudding user response:

If I understood you correctly, you need something like this:

List<Books> books = //load data;

foreach (book in books)
{
      if (!book.available)
      {
            //Your code
      }
}
  • Related