how do I ask an if argument on a specific field which in a list of objects that contain a list in c#?

David Fattal

I have a list of objects in which every object is containing a list itself. how do I get the the JellyFishID field or the Amount field for using an IF argument (I'm currently using Foreach):`

 public static List<Report> DataSorted = new List<Report> {
            new Report() { IsGoldUser=true, Date=new DateTime(2016, 3, 12,11, 59, 33), IsBurningWater=true, Type=Type.Shore, ZoneID = 1 ,
                ReportDetails =new List<ReportDetail> { new ReportDetail() { Amount = Amount.Few, Jellyfish = new Jellyfish { JellyfishID = 1, Venom = Venom.Strong } }  }  },
            new Report() { IsGoldUser=true, Date=new DateTime(2016, 3, 12, 11, 59, 33), IsBurningWater=true, Type=Type.Shore, ZoneID = 1 ,
                ReportDetails =new List<ReportDetail> { new ReportDetail() { Amount = Amount.Few, Jellyfish = new Jellyfish { JellyfishID = 1, Venom = Venom.Strong } }  }  },
            new Report() { IsGoldUser=true, Date=new DateTime(2016, 3, 12, 11, 59, 33), IsBurningWater=true, Type=Type.Shore, ZoneID = 1 ,
                ReportDetails =new List<ReportDetail> { new ReportDetail() { Amount = Amount.Few, Jellyfish = new Jellyfish { JellyfishID = 1, Venom = Venom.Strong } }  }  },
            new Report() { IsGoldUser=true, Date=new DateTime(2016, 3, 12, 11, 59, 33), IsBurningWater=true, Type=Type.Shore, ZoneID = 1 ,
                ReportDetails =new List<ReportDetail> { new ReportDetail() { Amount = Amount.Few, Jellyfish = new Jellyfish { JellyfishID = 1, Venom = Venom.Strong } }  }  },

 foreach (var item in DataSorted)
        {

               if (item.ReportDetails....) //???I want here to Make an Argument about The Amount field or the JellyFishID field in the list above....

        }
ventiseis

You don't describe exactly what you want to check, but with LINQ to Objects you have a lot of possiblities. At first, you need to reference the correct namespace with

using System.Linq;

at the top of your source code file.

Now, if you want to check if any items of your list contains a jellyfish with a given ID, you can use:

if (item.ReportDetails.Any(t => t.Jellyfish.JellyfishID == 1)) //...

Additionally you can have conditions inside a Where-function to filter your list and search only for jellyfish with a few amount:

if (item.ReportDetails.Where(t => t.Amount == Amount.Few).
                       Any(t => t.Jellyfish.JellyfishID == 1)) //...

There is a lot of information avaliable about LINQ, a lot of examples are in the MSDN (for example this intro page), but there are alternatives like this one: 101 Linq examples. It even has a tag on StackOverflow.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I remove duplicates from a list of dataclass-objects which each have a list as a field?

python3: how do i ask a user for input to select a specific list, (yes or no) and then print the items in that specific list in a random order?

How do I get the sum of specific attributes from objects in a list?

Hibernate criteria returning list which contain same objects, How to resolve it?

I have 3 lists of numbers, how do I make a list which will contain all the numbers by their indexes?

How to project a list of a specific field from a nested list of objects?

How can i update specific field from specific Objects in a dynamic list?

How do I find which columns in my pandas dataframe contain a list?

How do I create a list of columns which contain specified strings in their rows?

How do I loop over a list which consists of multiple objects and adding their properties in a seperate list?

How do I remove lines of a file which contain a specific string?

Searching for strings in a list which contain a specific word

How do I check if a List of Maps does *not* contain a specific key in Dart?

How can I sort the a list of strings which contain date as substring

How do I return the difference between a list of dictionaries and a list of objects based on a specific key:value only

How can I create a list of n user input objects (i.e. `...` ) in R which handles objects which do not exist?

How do I pass a list as an argument?

How do I place a symbol into an argument list

how do i comparison argument in two list

C# List of objects, how do I get the sum of a property

How do I send a list of objects through MessagingCenter in C#?

How to iterate over a list of objects which have a map as a field in jstl?

I have an object Company and List of Employee objects, I want to create a List of Company objects which will contain the single Employee Object

In cmd how can I list folders that contain files with a specific extension?

How to use useEffect hook with the dependency list as a specific field in an array of objects?

How can I select nodes that don't contain links but which do contain specific text using xpath

How do I remove float values from a list which has pandas series objects and floats

How do I query a list in a field?

How do I create a list of objects in Kotlin?