linq學習之join

來源:互聯網
上載者:User
using factory;namespace MyWebSiteTest{    public partial class linqtest : System.Web.UI.Page    {        static List<Customer> customers;        static List<Product> products;        static List<Order> orders;        public static void CreateEntities()        {            customers = new List<Customer>()            {                new Customer(){ CustomerId = 1, Name = "CA", Age=13},                new Customer(){ CustomerId = 2, Name = "CB", Age=13},                new Customer(){ CustomerId = 3, Name = "CC", Age=13},                new Customer(){ CustomerId = 4, Name = "CD", Age=13}            };            products = new List<Product>()            {                new Product(){ ProductId = 1, Name = "PA", Origin="P1" },                new Product(){ ProductId = 2, Name = "PB", Origin="P2" },                new Product(){ ProductId = 3, Name = "PC", Origin="P1" },                new Product(){ ProductId = 4, Name = "PD", Origin="P3" }            };            orders = new List<Order>()            {                new Order(){ OrderId = 1 , CustomerId =1,                     Products = new List<Product>{                         new Product(){ ProductId = 2, Name = "PB", Origin="P2" },                        new Product(){ ProductId = 3, Name = "PC", Origin="P1" }                    }},                new Order(){ OrderId = 2 , CustomerId =1,                     Products = new List<Product>{                         new Product(){ ProductId = 3, Name = "PC", Origin="P1" },                        new Product(){ ProductId = 4, Name = "PD", Origin="P3" }                    }},                new Order(){ OrderId = 3 , CustomerId =3,                     Products = new List<Product>{                         new Product(){ ProductId = 4, Name = "PD", Origin="P3" }                    }},                new Order(){ OrderId = 4 , CustomerId =2,                     Products = new List<Product>{                         new Product(){ ProductId = 1, Name = "PA", Origin="P1" },                        new Product(){ ProductId = 4, Name = "PD", Origin="P3" }                    }}            };        }        protected void Page_Load(object sender, EventArgs e)        {            System.Text.StringBuilder outhtml = new System.Text.StringBuilder();            CreateEntities();            /*常見的內串連:             * (1).串連條件:c.CustomerId equals o.CustomerId 只能使用 equals 不能用 =,==,等於 等表示。             * (2).條件順序:c.CustomerId equals o.CustomerId ,range variable: c 和c之前的順序不能顛倒。             */            var join_query = from c in customers                             join o in orders on c.CustomerId equals o.CustomerId                             where o.OrderId == 2                             select c;            join_query.ToList().ForEach(x => Response.Write(string.Format("Id:{0}, Name:{1}", x.CustomerId, x.Name) + " "));            Response.Write("<hr /><br />");            /*簡單的分組*/            var group_query = from c in customers                              join o in orders on c.CustomerId equals o.CustomerId into os                              select new { c, os };            group_query.ToList().ForEach(                x =>                {                    outhtml.AppendLine(string.Format("<strong>Id:</strong>{0}, <strong>Name:</strong>{1}", x.c.CustomerId, x.c.Name) + "<br />");                    x.os.ToList().ForEach(ch => { outhtml.AppendLine(string.Format(" --Order Id:{0}", ch.OrderId) + "<br />"); });                    //可以再次篩選或其它處理,如下:                    //(from tt in x.os select tt).ToList().ForEach(ch => { outhtml.AppendLine(string.Format(" --Order Id:{0}", ch.OrderId) + "<br />"); });                }            );            Response.Write(outhtml);            Response.Write("<br />");                        /*             * Left Join 我們在SQL裡經常用到,讓我們來看看LINQ裡怎麼實現它:             */            var lftjon_quer = from c in customers                              join o in orders on c.CustomerId equals o.CustomerId into os                              from newtag in os.DefaultIfEmpty(                                new Order { OrderId = 0, CustomerId = 0, Products = new List<Product>() }                              )                              select new { c, newtag };            lftjon_quer.ToList().ForEach(x => Response.Write(string.Format("Customer Id:{0}, Name:{1}--Order Id:{0}<br />", x.c.CustomerId, x.newtag.OrderId)));            Response.Write("<br />");        }    }}namespace factory{    class Customer    {        public int CustomerId { get; set; }        public string Name { get; set; }        public int Age { get; set; }    }    class Product    {        public int ProductId { get; set; }        public string Name { get; set; }        public string Origin { get; set; }    }    class Order    {        public int OrderId { get; set; }        public int CustomerId { get; set; }        public List<Product> Products { get; set; }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.