Asp.net Linq implements group query

Source: Internet
Author: User

First, create a person. cs class.
Public class person
{
Public string name
{Get; set ;}
Public int age
{Get; set ;}
Public string sex
{Get; set ;}
}
Next we will add data to this person class:
List <person> plist1 = new List <person> ();
 
Plist1.Add (new person {name = "cxx1", age = 24, sex = "male "});
Plist1.Add (new person {name = "www.baidu.com", age = 25, sex = "male "});
Plist1.Add (new person {name = "www.52mvc.com", age = 26, sex = "male "});
 
 
List <person> plist2 = new List <person> ();
 
 
Plist2.Add (new person {name = "cxx1", age = 24, sex = "male "});
Plist2.Add (new person {name = "cxx2", age = 28, sex = "male "});
Plist2.Add (new person {name = "cxx4", age = 27, sex = "male "});
Plist2.Add (new person {name = "cxx5", age = 28, sex = "male "});
// Asp.net
Var query = from person p in plist1
Join person per in plist2
On p. name equals per. name
Select new
{
Name = p. name,
Gender = p. sex,
Age = p. age
 
};
Gd2.DataSource = query;
Gd2.DataBind ();
 
The returned result is:
 
 

 
Note: This method is used to find data with the same name record in plist1 and plist2.
 
 
Next, let's take a look at how left join in SQL is implemented in linq.
Create two cs classes first.
 
/// <Summary>
/// Mobile phone list
/// </Summary>
Public class MobileStore
{
Public string mobId
{Set; get ;}
Public string mobName
{Set; get ;}
}
/// <Summary>
/// Mobile phone sales table
/// </Summary>
Public class MobileSale
{
Public string Sid
{Set; get ;}
Public string mobId
{Set; get ;}
Public string mobName
{Set; get ;}
Public string price
{Set; get ;}
}
 
 
List <MobileStore> listStore = new List <MobileStore> ();

ListStore. Add (new MobileStore {mobId = "1", mobName = "N86 "});
ListStore. Add (new MobileStore {mobId = "2", mobName = "N82 "});
ListStore. Add (new MobileStore {mobId = "3", mobName = "N81 "});
ListStore. Add (new MobileStore {mobId = "4", mobName = "N95 "});
ListStore. Add (new MobileStore {mobId = "5", mobName = "N85 "});
ListStore. Add (new MobileStore {mobId = "6", mobName = "N97 "});
 
List <MobileSale> listSale = new List <MobileSale> ();
ListSale. Add (new MobileSale {Sid = "1", mobId = "1", mobName = "N86", price = "100 "});
ListSale. Add (new MobileSale {Sid = "2", mobId = "2", mobName = "N82", price = "220 "});
ListSale. Add (new MobileSale {Sid = "3", mobId = "3", mobName = "N81", price = "300 "});
 
Var query = from MobileStore m in listStore
 
Join MobileSale sale in listSale
On m. mobId equals sale. mobId into joinm
 
From j in joinm. DefaultIfEmpty ()
Select new
{
ID = m. mobId,
Name = m. mobName,
Price = j = null? "No data": j. price,
};
Gd. DataSource = query;
Gd. DataBind ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.