首页 > 初学LINQ语句

初学LINQ语句

//有两个数组,客户和地址,他们之间通过公司名关联:var customers = new[] { new {CustomerID=1,FirstName="Kim",LastName="Abercrombie",CompanyName="Alpine Sky House"},new {CustomerID=2,FirstName="Jeff",LastName="Hay",CompanyName="Coho Winery"},new {CustomerID=3,FirstName="Charlie",LastName="Herb",CompanyName="Alpine Sky House"},new {CustomerID=4,FirstName="Chris",LastName="Preston",CompanyName="Trey Reseach"},new {CustomerID=5,FirstName="Dave",LastName="Barnett",CompanyName="Wingtip Toys"},new {CustomerID=6,FirstName="Ann",LastName="Beebe",CompanyName="Coho Winery"},new {CustomerID=7,FirstName="John",LastName="Kane",CompanyName="Wingtip Toys"},new {CustomerID=8,FirstName="David",LastName="Simpson",CompanyName="Trey Reseach"},new {CustomerID=9,FirstName="Greg",LastName="Abercrombie",CompanyName="Wingtip Toys"},new {CustomerID=10,FirstName="Tim",LastName="Abercrombie",CompanyName="Wide World Importers"}};var addresses = new[] { new {CompanyName="Alpine Sky House",City="Berne",Country="Switzerland"},new {CompanyName="Coho Winery",City="San Francisco",Country="United States"},new {CompanyName="Trey Reseach",City="New York",Country="United States"},new {CompanyName="Wingtip Toys",City="London",Country="United Kingdom"},new {CompanyName="Wide World Importers",City="Tetbury",Country="United Kingdom"}};//------------------筛选出customers数组中客户名(从枚举集合投射指定字段)----------------------//1.使用Select方法,用Lambda表达式标识要投射的字段(IEnumerable类可换var)IEnumerable<string> customerFirstNames = customers.Select(cust=>cust.FirstName);//2.使用from和select查询操作符var customerFirstNames = from custs in customers select custs.FirstName;//-----------------筛选出地址数组中国家是美国的公司名称(筛选来自可枚举集合的行)----------------------//1.使用Where方法,用Lambda表达式指定行的匹配条件var usCompanies=addresses.Where(addr=>String.Equals(addr.Country,"United States")).Select(usComp=>usComp.CompanyName);//2.使用where操作符var usCompanies=from addr in addresses where String.Equals(addr.Country,"United States") select addr.CompamyName;//----------------筛选出地址数组中公司名称并降序排序(按特定顺序枚举数据)--------------------------//1.使用OrderBy方法,用Lambda表达式标识用于对行进行排序的字段var companyNames=addresses.OrderBy(addr=>addr.CompanyName).Select(comp=>comp.CompanyName);//2.使用orderby查询操作符var companyNames=from addr in addresses orderby addr,CompanyName select addr.CompanyName; //--------------用国家对地址数组进行分组筛选(根据字段的值对数据进行分组)----------------------------//1.使用GroupBy方法,用Lambda表达式标识用于对行进行分组的字段var companiesGroupedByCountry=addresses.GroupBy(addrs=>Country);//2.使用group by查询操作符var companiesGroupedByCountry=from addr in addresses group addr by addr.Country; //--------------------关联两个数组查询(联接两个不同集合中的数据)-------------------------------//1.使用Join方法指定连接的集合、联接条件和结果字段var citiesAndCustomers=Customers.Select(c=>new{c.FirstName,c.LastName,c.CompanyName}).Join(addresses,custs=>custs.CompanyName,addrs=>addrs.CompanyName,(custs,addrs)=>new{custs.FirstName,custs.LastName,addrs.Country});//2.使用Join查询操作符var citiesAndCustomers=from a in addresses join c in customers on a.CompanyName equals c.CompanyName select new{c.FirstName,c.LastName,a.Country};

转载于:https://www.cnblogs.com/wangqilong/p/9417496.html

更多相关:

  • 菜鸟一枚,正在学习C++ Gui Qt4,整理很零碎,欢迎批评指正   1.窗口标题: QWidget *window = new QWidget; window->setWindowTitle("Enter Your Age"); **************************************** 关于标题...

  • 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 总体思路是: 比较两个链表头节点,较小的插入新链表指针之后,同时较小链表指针向后移动一位 实现如下: ListNode* mergeTwo...

  • 1.直接调用微软socket对象处理 static void Main(string[] args){try{IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });//在3721端口新建一个TcpListener对象TcpListener listener = new...

  •   现在很多地方都会用到zookeeper, 用到它的地方就是为了实现分布式。用到的场景就是服务注册,比如一个集群服务器,需要知道哪些服务器在线,哪些服务器不在线。   ZK有一个功能,就是创建临时节点,当机器启动应用的时候就会连接到一个ZK节点,然后创建一个临时节点,那么通过获取监听该路径,并且获取该路径下的节点数量就知道有哪些服务...

  • 前台到后台java时data日期类型的转化 在实体类中用@DataTimeFormat,这样设置即使传过来是空的字符串也是可以转的,要和前面传过来的格式一致,如 @XmlElement(name="BeginDate") @DateTimeFormat(pattern="yyyy-MM-dd") private Date begin...

  • //获取某一个cookie的值 const getCookie = key => {var k = key, dc = document.cookie;if (dc.length > 0) {var s = dc.indexOf(k + "=");if (s != -1) {s = s + k.length + 1;var e = d...

  • var SGheadMapPoints = {/*obj={ maxLng: minLng: maxLat: minLat: maxCount:最大人数 minCount:最小人数 total:点位数量 }*/get: function (obj) {var arr = [];obj.maxCount || (obj.maxCount...

  • //自动搜索指定的请柬 var alertTipText = "请柬找到了,就在这个网页里面,自己仔细看吧"; var delay = 1 * 1000;//1秒后循环下一页寻找 /*获取子DOM元素在父元素里面的索引位置(是第几个元素)*/ function getNodeListIndex(childNode) {return c...

  •  获取天气情况(不支持跨域) /*json原生获取*/ function getJSON() {var XML;var url = "http://wthrcdn.etouch.cn/weather_mini?city=杭州";if (window.XMLHttpRequest) {XML = new XMLHttpRequest(...