日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Sharepoint学习笔记---Linq to Sharepoint--查询语法

發(fā)布時(shí)間:2023/12/18 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sharepoint学习笔记---Linq to Sharepoint--查询语法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

??Linq to sharepoint的引入的確給我們操作Sharepoint帶來(lái)了便利,首先就體現(xiàn)在對(duì)Sharepoint 的查詢(xún)優(yōu)勢(shì)上。它基本可以照搬Linq to SQL的查詢(xún)語(yǔ)法,這就大大保護(hù)了你的學(xué)習(xí)成本,當(dāng)然,它們之間有某些差異(如:在List間的Left Outer Join等處)
? 在實(shí)踐本處的知識(shí)點(diǎn)前,首先需要搭建我們的測(cè)試環(huán)境。這里我們?nèi)耘f引入Northwind數(shù)據(jù)庫(kù)的數(shù)據(jù)表:Customers,Orders,Order Details和Products。我們要用到它們是因?yàn)槲覀円盟锩娴臄?shù)據(jù)快速創(chuàng)建出我們Sharepoint網(wǎng)站的List內(nèi)容(我們要在Sharepoint網(wǎng)站上創(chuàng)建出4個(gè)CustomerLists: ACustomer,AOrders,AOrderDetails和AProducts)。
? 如何搭建此環(huán)境,請(qǐng)參照

??Sharepoint學(xué)習(xí)筆記---SPList--使用Linq to Sharepoint間接查詢(xún)External List(1.通過(guò)BCS創(chuàng)建External List)

??Sharepoint學(xué)習(xí)筆記---SPList--使用Linq to Sharepoint間接查詢(xún)External List(2.復(fù)制External List內(nèi)容)

??Sharepoint學(xué)習(xí)筆記---SPList--使用Linq to Sharepoint間接查詢(xún)External List(3.使用Linq to Sharepoint查詢(xún)List內(nèi)容)
? 當(dāng)然你也可以想其它辦法創(chuàng)建相應(yīng)的Sharepoint List環(huán)境(eg:通過(guò)Excel表導(dǎo)入),總之,此處不再贅述如何搭建學(xué)習(xí)環(huán)境。
? 我們創(chuàng)建好的List如下

?

? 下面分別列舉查詢(xún)語(yǔ)法:
? 首先,在程序頂部我們定義了四個(gè)Entity變量并給它們賦值,即從Sharepoint網(wǎng)站的相應(yīng)List中取出List的內(nèi)容賦值給對(duì)應(yīng)的Entity Classes?

EntityList<ACustomerItem>?MyCustomers;
????????EntityList<AOrdersItem>?MyOrders;
????????EntityList<AOrderDetailsItem>?MyOrderDetails;
????????EntityList<AProductsItem>?MyProducts;
???????var?dc?=?new?NorthWindEntityDataContext(SPContext.Current.Web.Url);
????????MyCustomers?=?dc.GetList<ACustomerItem>("ACustomer");
????????MyOrders?=?dc.GetList<AOrdersItem>("AOrders");
????????MyOrderDetails?=?dc.GetList<AOrderDetailsItem>("AOrderDetails");
????????MyProducts?=?dc.GetList<AProductsItem>("AProducts");

?接下來(lái)就是使用上面的 MyCustomers,MyOrders,MyOrderDetails,MyProducts進(jìn)行各種查詢(xún)。?

?1.ACustomer中所有的CustomerID(Distinct查詢(xún))??

View Code var?distinctCustomers?=?(from?dcustom?in?MyCustomers?select?dcustom.BCSFindCustomerID).Distinct();

?2.查詢(xún)所有有定單的Customer?

View Code var?query?=?from?c?in?MyCustomers
?????????????where?(from?o?in?MyOrders
???????????????????????????????????select?o.BCSFindCustomerID).Contains(c.BCSFindCustomerID)
????????????????????????????select?c;

?3.查詢(xún)所有沒(méi)有定單的Customer?

View Code var?query?=?from?c?in?MyCustomers
????????????where?!(from?o?in?MyOrders
????????????????????????????????select?o.BCSFindCustomerID).Contains(c.BCSFindCustomerID)
????????????????????????select?new
????????????????????????{
????????????????????????????CopanyName?=?c.BCSFindCompanyName,
????????????????????????????ContanctName?=?c.BCSFindContactName,
????????????????????????????Address?=?new
????????????????????????????{
????????????????????????????????Country?=?c.BCSFindCountry,
????????????????????????????????City?=?c.BCSFindCity,
????????????????????????????????PostalCode?=?c.BCSFindPostalCode
????????????????????????????}

????????????????????????};

?4.判斷Customer的Country是否屬于歐洲國(guó)家

View Code var?query?=?from?c?in?MyCustomers
??????????????select?new
????????????????????????{
????????????????????????????CustomerName?=?c.BCSFindContactName,
????????????????????????????Country?=?c.BCSFindCountry,
????????????????????????????IsEuropeCountry?=?new?string[]?{?"Belgium",?"Denmark",?"Finland",?"France",?"Germany",?"Ireland",?"Italy",?"Norway",?"Poland",?"Portugal",?"Spain",?"Sweden",?"Switzerland",?"UK"?}.Contains(c.BCSFindCountry)???"Yes"?:?"No"
????????????????????????};

?

?5.按Customer的Country是否屬于歐洲國(guó)家進(jìn)行分組(Group),并統(tǒng)計(jì)每組包含的Customer數(shù)目?

View Code var?query?=?from?c?in?MyCustomers
?????????????group?c?by?new?{?IsEuropeCountry?=?new?string[]?{?"Belgium",?"Denmark",?"Finland",?"France",?"Germany",?"Ireland",?"Italy",?"Norway",?"Poland",?"Portugal",?"Spain",?"Sweden",?"Switzerland",?"UK"?}.Contains(c.BCSFindCountry)???"Yes"?:?"No"?}?into?g
????????????????????????select?new
????????????????????????{
????????????????????????????CountryAmount?=?g.Count(),
????????????????????????????IsEuropeCountry?=?g.Key.IsEuropeCountry
????????????????????????};

?

?6.按Customer的不同Country進(jìn)行分組,并顯示每個(gè)分組的Customer

View Code var?query?=?from?c?in?MyCustomers
?????????????join?g?in?query.ToList()?on?c.BCSFindCountry?equals?g.Country
?????????????????????????orderby?g.Country?descending
?????????????????????????select?new
?????????????????????????{
?????????????????????????????g.Country,
?????????????????????????????g.CustomerNumbers,
?????????????????????????????c.BCSFindContactName
?????????????????????????};

?

7.顯示含有5個(gè)以上Customer的Country

View Code var?query?=?from?c?in?MyCustomers
?????????????group?c?by?c.BCSFindCountry?into?g
????????????????????????where?g.Count()?>?5
????????????????????????orderby?g.Count()?descending
????????????????????????select?new
????????????????????????{
????????????????????????????Country?=?g.Key,
????????????????????????????CustomerNumbers?=?g.Count()
????????????????????????};

?

8.按Customer的Country與City進(jìn)行分組(Group)

View Code var?query?=?from?c?in?MyCustomers
????????????group?c?by?new?{?c.BCSFindCity,?c.BCSFindCountry?}?into?g
????????????????????????orderby?g.Key.BCSFindCountry,?g.Key.BCSFindCity
????????????????????????select?new
????????????????????????{
????????????????????????????Country?=?g.Key.BCSFindCountry,
????????????????????????????City?=?g.Key.BCSFindCity
????????????????????????};

?

9.對(duì)Customer進(jìn)行分頁(yè),并提取第二頁(yè)的Customer(Skip, Take)

View Code var?query?=?(from?c?in?MyCustomers
?????????????select?c).Skip(10).Take(10);

?

10.提取Country包含字母"A",ContactName以"A"開(kāi)頭的Customer (UnionJoin)

View Code var?query?=?(from?c?in?MyCustomers.ToList()
??????????????where?c.BCSFindCity.Contains("A")
?????????????????????????select?c).Union
???????????????????????????(from?c?in?MyCustomers
????????????????????????????where?c.BCSFindContactName.StartsWith("A")
????????????????????????????select?c).OrderBy(c?=>?c.BCSFindContactName).OrderBy(c?=>?c.BCSFindContactName);

?

11.提取Country包含字母"A",ContactName以"A"開(kāi)頭的Customer (ConcatJoin)?

View Code var?query?=?(from?c?in?MyCustomers.ToList()
??????????????where?c.BCSFindCity.Contains("A")
?????????????????????????select?c).Concat
??????????????????????????(from?c?in?MyCustomers
???????????????????????????where?c.BCSFindContactName.StartsWith("A")
???????????????????????????select?c).OrderBy(c?=>?c.BCSFindContactName).OrderBy(c?=>?c.BCSFindContactName);

?

12.提取Country包含字母"A",ContactName以"A"開(kāi)頭的Customer (InterSectJoin)

View Code var?query?=?(from?c?in?MyCustomers.ToList()
??????????????where?c.BCSFindCity.Contains("A")
?????????????????????????select?c).Intersect
??????????????????????????(from?c?in?MyCustomers
???????????????????????????where?c.BCSFindContactName.StartsWith("A")
???????????????????????????select?c).OrderBy(c?=>?c.BCSFindContactName).OrderBy(c?=>?c.BCSFindContactName);

?

13.提取Country包含字母"A",ContactName以"A"開(kāi)頭的Customer (ExceptJoin)

View Code var?query?=?(from?c?in?MyCustomers
???????????????where?c.BCSFindCity.Contains("A")
?????????????????????????select?c).Except
??????????????????????????(from?c?in?MyCustomers
???????????????????????????where?c.BCSFindContactName.StartsWith("A")
???????????????????????????select?c).OrderBy(c?=>?c.BCSFindContactName).OrderBy(c?=>?c.BCSFindContactName);

?

14.顯示有Order的Customer及他的Orders(Join)?

View Code var?query?=?from?c?in?MyCustomers.ToList()
????????????join?o?in?MyOrders?on?c.BCSFindCustomerID?equals?o.BCSFindCustomerID
????????????????????????select?new
????????????????????????{
????????????????????????????c.BCSFindCustomerID,
????????????????????????????c.BCSFindCompanyName,
????????????????????????????c.BCSFindContactName,
????????????????????????????c.BCSFindCountry,
????????????????????????????c.BCSFindCity,
????????????????????????????o.BCSFindOrderID,
????????????????????????????o.BCSFindEmployeeID,
????????????????????????????o.BCSFindShipCity,
????????????????????????????o.BCSFindShipCountry,
????????????????????????????o.BCSFindShipVia,
????????????????????????????o.BCSFindRequiredDate
????????????????????????};

?

15.顯示Customer及他的Orders(LeftJoin)

View Code var?query?=?from?c?in?MyCustomers.ToList()
?????????????join?o?in?MyOrders?on?c.BCSFindCustomerID?equals?o.BCSFindCustomerID
????????????????????????into?leftjoin
????????????????????????from?fnresult?in?leftjoin.DefaultIfEmpty()
????????????????????????select?fnresult;

?

16.顯示Order數(shù)大于5的Customer

View Code var?query?=?(from?c?in?MyCustomers.ToList()
??????????????join?o?in?MyOrders?on?c.BCSFindCustomerID?equals?o.BCSFindCustomerID
?????????????????????????where?(from?o1?in?MyOrders
????????????????????????????????group?o1?by?o1.BCSFindCustomerID?into?g
????????????????????????????????where?g.Count()?>?5
????????????????????????????????select?g.Key).Contains(c.BCSFindCustomerID)
?????????????????????????select?new
?????????????????????????{
?????????????????????????????c.BCSFindCustomerID,
?????????????????????????????c.BCSFindContactName,
?????????????????????????????o.BCSFindOrderID
?????????????????????????}).OrderBy(c?=>?c.BCSFindContactName);

?

17.獲取指定用戶(hù)"ALFKI"的Order

View Code var?query?=?from?c?in?MyCustomers.ToList()
????????????join?o?in?MyOrders?on?c.BCSFindCustomerID?equals?o.BCSFindCustomerID
????????????????????????where?c.BCSFindCustomerID?==?"ALFKI"
????????????????????????select?new
????????????????????????{
????????????????????????????c.BCSFindCustomerID,
????????????????????????????c.BCSFindCompanyName,
????????????????????????????c.BCSFindContactName,
????????????????????????????c.BCSFindCountry,
????????????????????????????c.BCSFindCity,
????????????????????????????o.BCSFindOrderID,
????????????????????????????o.BCSFindEmployeeID,
????????????????????????????o.BCSFindShipCity,
????????????????????????????o.BCSFindShipCountry,
????????????????????????????o.BCSFindShipVia,
????????????????????????????o.BCSFindRequiredDate
????????????????????????};

?

18.獲取指定用戶(hù)"ALFKI"的Order,并計(jì)算每個(gè)Order的總金額BCSFindUnitPrice * BCSFindQuantity

View Code var?query?=?from?c?in?MyCustomers.ToList()
?????????????join?o?in?MyOrders?on?c.BCSFindCustomerID?equals?o.BCSFindCustomerID
????????????????????????join?cd?in?MyOrderDetails?on?o.BCSFindOrderID?equals?cd.BCSFindOrderID
????????????????????????where?c.BCSFindCustomerID?==?CustomerIDstr
????????????????????????select?new
????????????????????????{
????????????????????????????c.BCSFindCustomerID,
????????????????????????????c.BCSFindCompanyName,
????????????????????????????c.BCSFindContactName,
????????????????????????????c.BCSFindCountry,
????????????????????????????c.BCSFindCity,
????????????????????????????o.BCSFindOrderID,
????????????????????????????o.BCSFindEmployeeID,
????????????????????????????o.BCSFindShipCity,
????????????????????????????o.BCSFindShipCountry,
????????????????????????????o.BCSFindShipVia,
????????????????????????????o.BCSFindRequiredDate,
????????????????????????????cd.BCSFindProductID,
????????????????????????????cd.BCSFindQuantity,
????????????????????????????cd.BCSFindDiscount,
????????????????????????????cd.BCSFindUnitPrice,
????????????????????????????TotalCost?=?cd.BCSFindUnitPrice?*?cd.BCSFindQuantity
????????????????????????};

?

19.獲取指定用戶(hù)"ALFKI"所Order的產(chǎn)品的有關(guān)信息(3 tables)

View Code var?query1?=?from?c?in?MyCustomers.ToList()
?????????????join?o?in?MyOrders?on?c.BCSFindCustomerID?equals?o.BCSFindCustomerID
?????????????????????????join?cd?in?MyOrderDetails?on?o.BCSFindOrderID?equals?cd.BCSFindOrderID
?????????????????????????join?p?in?MyProducts?on?cd.BCSFindProductID?equals?p.BCSFindProductID
?????????????????????????where?c.BCSFindCustomerID?==?"ALFKI"
?????????????????????????select?new
?????????????????????????{
?????????????????????????????c.BCSFindCustomerID,
?????????????????????????????o.BCSFindOrderID,
?????????????????????????????p.BCSFindProductName,
?????????????????????????????cd.BCSFindQuantity,
?????????????????????????????c.BCSFindCompanyName,
?????????????????????????????c.BCSFindContactName,
?????????????????????????????c.BCSFindCountry,
?????????????????????????????c.BCSFindCity,
?????????????????????????????o.BCSFindEmployeeID,
?????????????????????????????o.BCSFindShipCity,
?????????????????????????????o.BCSFindShipCountry,
?????????????????????????????o.BCSFindShipVia,
?????????????????????????????o.BCSFindRequiredDate,
?????????????????????????????cd.BCSFindProductID,
?????????????????????????????cd.BCSFindDiscount,
?????????????????????????????cd.BCSFindUnitPrice,
?????????????????????????????p.BCSFindSupplierID
?????????????????????????};

?

20.獲取Order了產(chǎn)品"CHAI"的用戶(hù)

View Code var?query?=?from?c?in?MyCustomers
?????????????where?(from?c1?in?MyCustomers.ToList()
???????????????????????????????join?o?in?MyOrders?on?c1.BCSFindCustomerID?equals?o.BCSFindCustomerID
???????????????????????????????join?cd?in?MyOrderDetails?on?o.BCSFindOrderID?equals?cd.BCSFindOrderID
???????????????????????????????join?p?in?MyProducts?on?cd.BCSFindProductID?equals?p.BCSFindProductID
???????????????????????????????where?p.BCSFindProductName?==?"Chai"
???????????????????????????????select?c1.BCSFindCustomerID).Contains(c.BCSFindCustomerID)
????????????????????????select?c;

?

21.獲取Order了產(chǎn)品"CHAI"的用戶(hù)以及他們所Order的產(chǎn)品"CHAI"的OrderDetails

View Code var?query?=?from?c?in?MyCustomers.ToList()
????????????join?o?in?MyOrders?on?c.BCSFindCustomerID?equals?o.BCSFindCustomerID
????????????????????????join?cd?in?MyOrderDetails?on?o.BCSFindOrderID?equals?cd.BCSFindOrderID
????????????????????????join?p?in?MyProducts?on?cd.BCSFindProductID?equals?p.BCSFindProductID
????????????????????????where?(from?c1?in?MyCustomers.ToList()
???????????????????????????????join?o1?in?MyOrders?on?c1.BCSFindCustomerID?equals?o1.BCSFindCustomerID
???????????????????????????????join?cd1?in?MyOrderDetails?on?o1.BCSFindOrderID?equals?cd1.BCSFindOrderID
???????????????????????????????join?p1?in?MyProducts?on?cd1.BCSFindProductID?equals?p1.BCSFindProductID
???????????????????????????????where?p1.BCSFindProductName?==?"Chai"
???????????????????????????????select?c1.BCSFindCustomerID).Contains(c.BCSFindCustomerID)
???????????????????????????????&&
???????????????????????????????p.BCSFindProductName?==?"Chai"
????????????????????????select?new
????????????????????????{
????????????????????????????customerName?=?c.BCSFindContactName,
????????????????????????????ProductName?=?p.BCSFindProductName,
????????????????????????????UnitPrice?=?cd.BCSFindUnitPrice,
????????????????????????????Quantity?=?cd.BCSFindQuantity,
????????????????????????????SellTotal?=?cd.BCSFindUnitPrice?*?cd.BCSFindQuantity

????????????????????????};

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/wsdj-ITtech/archive/2011/11/03/2232530.html

總結(jié)

以上是生活随笔為你收集整理的Sharepoint学习笔记---Linq to Sharepoint--查询语法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。