LINQ取复杂列表

    class Program
    {
        public class Order
        {
            public int ID { get; set; }

            public string OrderNo { get; set; }
        }

        public class OrderDetail
        {
            public int ID { get; set; }

            public int OrderID { get; set; }

            public int ProductId { get; set; }
        }

        public class Product
        {
            public int ID { get; set; }
            public string Images { get; set; }
        }
        static void Main(string[] args)
        {
            //SendSMS();
            //Charge();
            var orderList = new List<Order>()
            {
                new Order(){ ID=1 ,OrderNo = "aaa"},
                new Order(){ ID=2 ,OrderNo = "bbb"}
            };
            var orderdetaillist = new List<OrderDetail>(){
                new OrderDetail(){ ID=1, OrderID=1, ProductId = 1},
                new OrderDetail(){ ID=2, OrderID=1, ProductId = 3},
                new OrderDetail(){ ID=3, OrderID=1, ProductId = 4},

                new OrderDetail(){ ID=4, OrderID=2, ProductId = 2},
                new OrderDetail(){ ID=5, OrderID=2, ProductId = 5},
            };
            var productList = new List<Product>(){
            new Product(){ ID=1,Images = "111"},
            new Product(){ ID=2,Images = "222"},
            new Product(){ ID=3,Images = "333"},
            new Product(){ ID=4,Images = "444"},
            new Product(){ ID=5,Images = "555"}
            };

            var list = (from order in orderList
                        join yo in
                            (from d in orderdetaillist
                             join p in productList on d.ProductId equals p.ID
                             select new { d.OrderID, p.Images }) on order.ID equals yo.OrderID into gg
                        select new
                        {
                            order.OrderNo,
                            Images = (from fuck in gg select fuck.Images).ToList()
                        }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine("订单号:" + item.OrderNo);
                Console.WriteLine("产品图片:");
                foreach (var pic in item.Images)
                {
                    Console.WriteLine("     " + pic);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
}

效果如下:

时间: 2024-12-13 09:54:24

LINQ取复杂列表的相关文章

php 抓取微信列表中的最新的一组微信消息

<?php $_G['wx_g'] = array('init' => array( "wx_content" => array("weixin_user" => "微信号码", "weixin_pass" => "微信密码") ) ); wx_login(); $messge_list = get_message_list(); $file_id=$messge_list['

jquery选择器多值情况处理(取select列表选项的值)

//多个同名的select值 var s=$("select[name=dutylistall]"); var dutylistall=""; s.each(function(i,el){ //console.info(i); //console.info(el); //console.info($(el).find("option:selected").val()); if(i!=0){ dutylistall+=",";

使用LINQ获取List列表中的某个字段值

使用LINQ获取列表中的某个字段值,下面以获取员工列表中的编号字段为例子. 1.使用Select方法 1 List<Emplayee> emplayeeList = GetEmplayeeList(); //获取员工信息列表 2 int[] empIds = emplayeeList.Select(a => a.Id).ToArray(); //获取员工编号数组 2.使用ForEach方法 1 List<Emplayee> emplayeeList = GetEmplayee

用requests库和BeautifulSoup4库爬取新闻列表

1.用requests库和BeautifulSoup4库,爬取校园新闻列表的时间.标题.链接.来源. import requests from bs4 import BeautifulSoup mt="http://news.gzcc.cn/html/xiaoyuanxinwen/" res=requests.get(mt) res.encoding='utf-8' soup=BeautifulSoup(res.text,"html.parser") for new

requests库和BeautifulSoup4库爬取新闻列表

画图显示: import jieba from wordcloud import WordCloud import matplotlib.pyplot as plt txt = open("zuihou.txt","r",encoding='utf-8').read() wordlist = jieba.lcut(txt) wl_split=" ".join(wordlist) mywc = WordCloud().generate(wl_spl

用Linq取两个数组的差集

两个数组,取其差集,用Linq做比较方便,效率也比较高,具体如下示例 有两个数组list1 和list2 ,如下 List<int> list1 = new List<int>(); list1.Add(1); list1.Add(2); list1.Add(3); List<int> list2 = new List<int>();list2.Add(1); list2.Add(2); list2.Add(3); list2.Add(4); list2.A

爬虫实例1-爬取新闻列表和发布时间

一.新建工程 scrapy  startproject shop 二.Items.py文件代码: import scrapy class  ShopItem(scrapy.Item): title = scrapy.Field() time = scrapy.Field() 三.shopspider.py文件爬虫代码 #  -*-coding:UTF-8-*- import scrapy from shop.items  import ShopItem class  shopSpider(scr

xmpp-iOS-获取好友列表、聊天信息列表、聊天

typedef enum {//发送消息类型的枚举 text, image, audio } MsgType; #import "XMPPData.h" #import "XMPPFramework.h" @interface XMPPData()<XMPPStreamDelegate,XMPPRosterDelegate,NSFetchedResultsControllerDelegate> @property(strong,nonatomic) XM

T-SQL,LINQ取集合当中的重复项

T-SQL: select * from Articles where Title in(Select Title From Articles Group By title Having Count(*) > 1) order by title LINQ: from t in Articles where (from t1 in Articles group t1 by t1.Title into groupT where groupT.Count()>1 select groupT.Key)