How to get IEnumerable <T1> of members of class T2 to Enumerable <T2> using Linq? (FROM#)

Suppose I have:

 public class foobar
 {
    public int lorem;
    public int ipsum;
 }

 IEnumerable<foobar> items = new List<foobar>();
 items.add(new foobar(){lorem = 0, ipsum = 0};
 items.add(new foobar(){lorem = 1, ipsum = 1};

      

How can I get IEnumerable from all "lorem" in "elements" using Linq?

+1


a source to share


2 answers


Try to run



var allLorems = items.Select(x => x.lorem);

      

+4


a source


I think you mean:

 List<foobar> items = new List<foobar>();
 items.Add(new foobar(){lorem = 0, ipsum = 0});
 items.Add(new foobar(){lorem = 1, ipsum = 1});

      



and then:

var lorems=from i in items select i.lorem;

      

+2


a source







All Articles