1

I have a custom object defined as follows:

public class Testobj
{
    public double Campo1 { get; set; }
    public int Campo2 { get; set; }
    public string Stringa1 { get; set; }
    public Testobj() { }
}

Then, I build a List<Testobj> with these elements:

        List<Testobj> listobj = new List<Testobj>();
        Testobj to1 = new Testobj() { Campo1 = 0.0, Campo2 = 1, Stringa1 = "cudumar0" };
        Testobj to2 = new Testobj() { Campo1 = 1.0, Campo2 = 1, Stringa1 = "cudumar1" };
        Testobj to3 = new Testobj() { Campo1 = 2.0, Campo2 = 1, Stringa1 = "cudumar2" };
        Testobj to4 = new Testobj() { Campo1 = 3.0, Campo2 = 3, Stringa1 = "cudumar3" };
        Testobj to5 = new Testobj() { Campo1 = 4.0, Campo2 = 3, Stringa1 = "cudumar4" };
        Testobj to6 = new Testobj() { Campo1 = 5.0, Campo2 = 3, Stringa1 = "cudumar5" };
        Testobj to7 = new Testobj() { Campo1 = 6.0, Campo2 = 2, Stringa1 = "cudumar6" };
        listobj.Add(to1);
        listobj.Add(to2);
        listobj.Add(to3);
        listobj.Add(to4);
        listobj.Add(to5);
        listobj.Add(to6);
        listobj.Add(to7);

I now need to select from listobj a sublist based on its elements' values. basycally, I need to get something like this:

List<Testobj> NewList = listobj.Select(...? );

I tried with

var query1 = listobj.Select(pr => pr.Campo2);

and also with .Where() or .SelectMany() but I can't understand or find any clearer information on how to use these functions. Thank you.

  • 1
    If you want a sub list then you need to use `Where` as that will filter. What exactly should your sublist look like? As an example `listoby.Where(x => x.Campo2 > 1)` would give you all the items where `Campo2` is greater than 1. – juharr Feb 16 '18 at 12:27
  • 1
    Read https://stackoverflow.com/questions/16322/learning-about-linq and have a look at https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b and use https://www.linqpad.net/ to experiment. it comes with samples to play around with. – Patrick Artner Feb 16 '18 at 12:30
  • I think what you need is `GroupBy` or `ToDictionary()` Linq methods. – mrogal.ski Feb 16 '18 at 12:35

3 Answers3

2

Where is the function you're looking for. It gives you each object, and you return true or false depending on whether you want it included in your sublist or not - list.Where(x => x.Campo2 > 100) if you only want the elements that have the value of Campo2 higher than 100.

Select is for transforming objects - if you want to just return a field on your sublist and not the entire element, you'd do list.Select(x => x.Campo2) to build an IEnumerable<int> out of all the elements of list.

You can combine the two to filter and transform. For example, list.Where(x => x.Campo2 > 100).Select(x => x.Campo1) to build an IEnumerable<double> made of all the Campo1 fields in those elements that have Campo2 > 100.

If you need to then turn the result into a List<>, you can call .ToList() at the end of your chain of filter and transform methods.

Andreia Gaita
  • 518
  • 3
  • 6
1

The line you are looking for is (Pseudo):

List<Testobj> filteredList = listobj.Where(x => x.Campo1.SomeProperty == 1.1m && x.Campo2 > 2 || x.Stringa1.Equals("cudumar0") ).ToList();

you can remove, modify and add all kinds of conditions, based on the logic operator & and |.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Morten Bork
  • 1,413
  • 11
  • 23
  • `where` is not a method in C#, it's a keyword used in old Linq queries and from what I remember there's no `equals` method in a `string` in C#, there's `Equals` instead. – mrogal.ski Feb 16 '18 at 12:39
  • Ah, I didn't capitalize them. I edited it so they are. Still, I said we aren't a code writing service. He can just use his mind, or intellisense and fix it. I promise to put more care in my answer, when he puts more care into his question. – Morten Bork Feb 16 '18 at 12:57
  • It's not about the question, it's about to know the language. Nowadays everyone is using IDEs which will do many things for you and many "developers" cannot even write a single line of code without it. Please put more care in every line of code you're writing. – mrogal.ski Feb 16 '18 at 13:11
  • You are seriously going to take the route that an IDE is a detriment? really? – Morten Bork Feb 16 '18 at 17:47
  • @mrogal.ski please also note, (Pseudo) or do you not know what that means? – Morten Bork Jan 15 '23 at 21:41
0

You can use LINQ operation to get the results. You can read more about how to use it here

Proper Linq where clauses

The following code will return all the objects where Campo2 > 2

List<Testobj> NewList = listobj.Where((x => x.Campo2>2).ToList();
Sudeep Reddy
  • 611
  • 7
  • 8