Pick and choose a lot of differences

I read a lot of sites / streams of choice and highlighted many of LINQ, but still don't quite understand.

Does one item in the collection select and choose to smooth the collection a lot (e.g. List> ())?

thanks

+6
linq
source share
3 answers

Here is an example. Hope he clarifies everything:

static void MethodRun() { List<Topping> testToppings = new List<Topping> { Topping.Cheese, Topping.Pepperoni, Topping.Sausage }; var firstLetterofToppings = testToppings.Select(top => top.ToString().First()); // returns "C, P, S" var singleToppingPizzas = testToppings.Select(top => new Pizza(top)).ToArray(); // returns "Pizza(Cheese), Pizza(Pepperoni), Pizza(Sausage)" List<Topping> firstPizza = new List<Topping> { Topping.Cheese, Topping.Anchovies }; List<Topping> secondPizza = new List<Topping> { Topping.Sausage, Topping.CanadianBacon, Topping.Pepperoni }; List<Topping> thirdPizza = new List<Topping> { Topping.Ham, Topping.Pepperoni }; List<IEnumerable<Topping>> toppingsPurchaseOrder = new List<IEnumerable<Topping>> { firstPizza, secondPizza, thirdPizza }; var toppingsToOrder = toppingsPurchaseOrder.SelectMany(order => order); //returns "Cheese, Anchovies, Sausage, CanadianBacon, Pepperoni, Ham, Pepperoni" } class Pizza { public List<Topping> Toppings { get; private set; } public Pizza(Topping topping) : this(new List<Topping> { topping }) { } public Pizza(IEnumerable<Topping> toppings) { this.Toppings = new List<Topping>(); this.Toppings.AddRange(toppings); } } enum Topping { Cheese, Pepperoni, Anchovies, Sausage, Ham, CanadianBacon } 

The key is that Select () can select any type of object. It is true that you can select a property of any general value assigned to your collection, but you can also select any other type of object. SelectMany () just aligns your list.

+5
source share

SelectMany returns the number of objects for each object passed into the expression. Select istead returns a single object for each object passed into the expression.

To quote the documentation:

Choose a lot

Designs each element of the sequence in IEnumerable <(Of <(T>)>) and aligns the resulting sequences into one sequence.

Select

Projects each element of a sequence into a new form.

You can use SelectMany if you want to smooth the hierarchy. For example. if you have Orders and OrderDetails . If you want to make a selection based on orders, but you want to use OrderDetails SelectMany as the return.

 var result = db.Orders .Where(x => x.CustomerId == 500) // input to next expression is IEnumerable<Order> .SelectMany(x => x.OrderDetails) // input to next expression is IEnumerable<OrderDetails> .Sum(x => x.PositionTotal); var result = db.Orders .Where(x => x.CustomerId == 500) // input to next expression is IEnumerable<Order> .Select(x => CustomerName); 
+3
source share

Go alternately on your question:

1. Does the item select one item in the collection? -> No, not at all. 'select' returns exactly the same number of elements that are in the collection, but in a different form (if required).

But, yes, it returns a single sequence (/ collection) containing all of these elements (for example, even_sqare in the example below).

eg
int[] even = {2,4}; int[] even_square = even.Select(n=> n*2).ToArray();

o / r
even_square to {4,8} , which is the same in counts (2), but with a different projection, we gave each squares by selecting them.

2. and chooses to smooth the collection a lot (for example, List> ())?

-> yes, but in reality, it looks like a cross connecting with our control.

  int[] odd = { 1, 3 }; int[] even = { 2, 4 }; int[] crossjoin = even.SelectMany( n => odd, //combining even with odd (n,o)=>Convert.ToInt32(n.ToString()+o.ToString())//given many selects, decide our projection ).ToArray(); foreach (var item in crossjoin) { Console.WriteLine(item); } Output: 21 23 41 43 

Now a million dollar thing:

  • Unlike most operators in linq, SelectMany accepts two collections, not one.
  • β€œeven” is the first set, and odd is the second set, which we passed using lambda, such as β€œn => odd”. (here people say that it is smoothed out).
  • The third parameter (in the previous statement, the second parameter) is TResult, which gives us an output that is a cross-connection and that is the beauty of SelectMany if we understand this.
  • Enjoy the learning.
0
source share

All Articles