Linq101-Element

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4
 5 namespace Linq101
 6 {
 7     class Element
 8     {
 9         /// <summary>
10         /// This sample uses First to return the first matching element as a Product, instead of as a sequence containing a Product.
11         /// </summary>
12         public void Linq58()
13         {
14             List<Data.Product> products = Data.GetProductList();
15
16             Data.Product product12 = (from p in products
17                                       where p.ProductID == 12
18                                       select p).First();
19
20             ObjectDumper.Write(product12);
21         }
22
23         /// <summary>
24         /// This sample uses First to find the first element in the array that starts with ‘o‘.
25         /// </summary>
26         public void Linq59()
27         {
28             string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
29
30             string startsWithO = strings.First(s => s[0] == ‘o‘);
31
32             Console.WriteLine("A string starting with ‘o‘: {0}", startsWithO);
33         }
34
35         /// <summary>
36         /// This sample uses FirstOrDefault to try to return the first element of the sequence, unless there are no elements, in which case the default value for that type is returned.
37         /// </summary>
38         public void Linq60()
39         {
40             int[] numbers = { };
41
42             int firstNumOrDefault = numbers.FirstOrDefault();
43
44             Console.WriteLine(firstNumOrDefault);
45         }
46
47         /// <summary>
48         /// This sample uses FirstOrDefault to return the first product whose ProductID is 789 as a single Product object, unless there is no match, in which case null is returned.
49         /// </summary>
50         public void Linq61()
51         {
52             List<Data.Product> products = Data.GetProductList();
53
54             Data.Product product789 = products.FirstOrDefault(p => p.ProductID == 789);
55
56             Console.WriteLine("Product 789 exists: {0}", product789 != null);
57         }
58
59         /// <summary>
60         /// This sample uses ElementAt to retrieve the second number greater than 5 from an array.
61         /// </summary>
62         public void Linq62()
63         {
64             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
65
66             int number = (from n in numbers
67                           where n > 5
68                           select n).ElementAt(1);
69
70             Console.WriteLine("Second number > 5: {0}", number);
71         }
72     }
73 }
时间: 2024-11-29 09:45:24

Linq101-Element的相关文章

215. Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.Note:You may assume k is always valid, 1 ≤ k ≤ array's le

LeetCode OJ 162. Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

LeetCode(7): Majority Element

Majority Element: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element always exist in the array. 题意:找出给定数组中的

[LeetCode]Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. Credits: Special thanks to @

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

Bentley GeoStructural Finite Element Analysis(FEM) v17.00.33.00 1CD

Systat.PeakFit.v4.12.00 1CD Autodesk.CADDoctor.For.Autodesk.Simulation.v2015.Win64-ISO 1DVD Autodesk.Vault.Basic.v2015-ISO 1DVD Autodesk.Vault.Workgroup.v2015-ISO 1DVD Command.Digital.AutoHook.2015.v0.8.0.60.beta.1 1CD OmniCAD.v1.0.0.2125.for.Siemens

- Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead. 解决方案

<template> <div>{{hello}}</div> <button @click="addOne">add one</button> <button @click="minusOne">minus one</button> </template> 在*.vue组件里有这么一段. 报错信息: (Emitted value instead of an instan

EventTarge Node Docuement Element HTMLElement 关系

综述: 可以将其看做是依次继承的关系: Node Node A Node is an interface from which a number of DOM types inherit, and allows these various types to be treated (or tested) similarly(好几种dom类型都继承自node接口,这些dom类型对外会表现的很相似). The following interfaces(下面的这些接口都继承自Node的方法和属性,包括D

162. Find Peak Element (Array; Divide-and-Conquer)

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi