English 中文(简体)
选择和选择许多区别
原标题:Select and select many difference
  • 时间:2010-03-11 14:13:31
  •  标签:
  • linq

我已经读到许多网站/读物,选择和选择许多网站,但还看不出。

是否在收集中选择一个内容,并选择许多统一的收集方法(例如清单和(或))?

增 编

问题回答

这里有一个样本。 希望能澄清:

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
}

关键是选择()可以选择任何类型的物体。 确实,你可以选择任何具有任何通用价值的财产,但也可以选择任何其他种类的物品。 选择选择选择(Many)只是对你的名单作一改动。

每一物体的“号”号标注均以表示。 每一物体的选取回single Object

引用文件:

www.un.org/Depts/DGACM/index_spanish.htm 选择了许多

各个项目按顺序排列,并按顺序排列。

<>Select

项目按顺序排列的每个要素都属于新形式。

如果你想要统一等级,你可以使用<代码>。 E.g. 如果您有Orders OrderDetails。 如果您希望根据这些命令作出选择,但您希望作为回报加入<>OrderDetails。

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);

请允许我依次回答你的问题:

1. Does select return one element in a collection? -> No, absolutely not. select returns exactly the same number of elements that are in the collection, but into different shape(if wanted).

但是,是的,它交还了所有这些因素的顺序(收集/收集)(例如,在下文中甚至_)。

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

o/p
even_square outputs to {4,8} which is exact same in counts (2), but different projection, we gave squares of each by selecting them.

2. & does 选择许多固定藏书(如:清单和/或)?

- ;是,但实际上,它像一个十字路口加入我们的控制。

 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

www.un.org/Depts/DGACM/index_spanish.htm 现在有100万美元:

  • Unlike most operators in linq, SelectMany takes two Collections, instead of one.
  • "even" is first collection, and odd is the second collection, which we passed in using lambda like this n = > odd . (this is where people say it flattens)
  • third parameter(in above statement, second parameter) is TResult which gives us output which is cross join and that s beauty of SelectMany if we understood it.
  • Enjoy learning.




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签