初识Web框架--Linq技术有那么深奥吗

Web开发框架中应用了大量.net 新特性,比方说Linq,它有那么深奥吗,下面我们以一段代码给大家深入讲解linq究竟是何方神圣。
源码
语法
分步解说
示例翻译后的sql语句
源码

源码

语法关键字解释
关键字 说明
from 查询的范围或数据源
where 根据条件表达式从数据源中查找数据
select 指定查询结果中的元素所具有的类型或表现形式
group 对查询结果按照键值对进行分组(IGrouping<TKey,TElement>)
into 提供一个标识符,它可以充当对join、group或select子句结果的引用
orderby 对查询出的元素进行排序(ascending/descending)
join 按照两个指定匹配条件来Equals连接两个数据源
let 产生一个用于存储查询表达式中的子表达式查询结果的范围变量
分步解说:.Table

var query = _productRepository.Table;
.Table定义:IQueryable<T> Table { get; }
.Table实现:
public virtual IQueryable<t>IQueryable<T> Table
{
get
{
return this.Entities;
}
}
从上面代码展示中我们看到IQueryable关键词,我们看下msdn中的解释:提供针对特定数据源(其中数据类型未未知)评估查询的功能。
IQueryable语法
IQueryable<t>接口仅供查询提供程序实现。
此接口继承 IEnumerable<t> 接口,因此,如果它表示一个查询,可以枚举该查询的结果。 枚举强制与关联的表达式树 IQueryable<t> 要执行对象。 执行不返回可枚举结果的查询时 Execute<tresult>(Expression) 调用方法

分步解说:.Where

.Where定义:public static IEnumerable<tsource> Where<tsource>(this IEnumerable<tsource> source, Func<tsource, bool> predicate);
Func<tsource, bool> predicate 断言,一个委托函数,具体实现过滤条件方法体。

分步解说:from p in query

from x in xxx,查询的数据源,对应sql语句from table。

分步解说:select p

select,关键词select查询的结果集,可以是单个属性,可以是属性集合,可以是类型,对应sql语句的select 。

分步解说:.OrderByDescending

OrderByDescending定义:public static IOrderedQueryable<tsource> OrderByDescending<tsource, tkey>(this IQueryable<tsource> source, Expressione<funce<tsource, tkey>> keySelector);
大家可以看到函数定义中除了IQureyable外有个Expressione类型,我们看它的定义 <tdelegate> : LambdaExpression,我们的Lambda表达式出现了,(Lambda表达式更多请参见 初识Web框架--C# Linq技术基础
OrderByDescending顾名思义是排序,为我们的查询结果按我们的义务意愿来进行排序。

示例翻译后的sql语句

我们在最后var products = new PagedList<product>(query, pageIndex, pageSize)这句语句中执行,从BS开发平台后台我们可以看到最终执行的sql语句是这样的:

SELECT 
[Extent1].[Id] AS [Id],
[Extent1].[ProductTypeId] AS [ProductTypeId],
[Extent1].[ParentGroupedProductId] AS [ParentGroupedProductId],
[Extent1].[VisibleIndividually] AS [VisibleIndividually],
[Extent1].[Name] AS [Name],
[Extent1].[ShortDescription] AS [ShortDescription],
[Extent1].[FullDescription] AS [FullDescription],
[Extent1].[AdminComment] AS [AdminComment],
[Extent1].[ProductTemplateId] AS [ProductTemplateId],
[Extent1].[VendorId] AS [VendorId],
[Extent1].[ShowOnHomePage] AS [ShowOnHomePage],
[Extent1].[MetaKeywords] AS [MetaKeywords],
[Extent1].[MetaDescription] AS [MetaDescription],
[Extent1].[MetaTitle] AS [MetaTitle],
[Extent1].[AllowCustomerReviews] AS [AllowCustomerReviews],
[Extent1].[ApprovedRatingSum] AS [ApprovedRatingSum],
[Extent1].[NotApprovedRatingSum] AS [NotApprovedRatingSum],
[Extent1].[ApprovedTotalReviews] AS [ApprovedTotalReviews],
[Extent1].[NotApprovedTotalReviews] AS [NotApprovedTotalReviews],
[Extent1].[SubjectToAcl] AS [SubjectToAcl],
[Extent1].[LimitedToStores] AS [LimitedToStores],
[Extent1].[Sku] AS [Sku],
[Extent1].[ManufacturerPartNumber] AS [ManufacturerPartNumber],
[Extent1].[Gtin] AS [Gtin],
[Extent1].[IsGiftCard] AS [IsGiftCard],
[Extent1].[GiftCardTypeId] AS [GiftCardTypeId],
[Extent1].[RequireOtherProducts] AS [RequireOtherProducts],
[Extent1].[RequiredProductIds] AS [RequiredProductIds],
[Extent1].[AutomaticallyAddRequiredProducts] AS [AutomaticallyAddRequiredProducts],
[Extent1].[IsDownload] AS [IsDownload],
[Extent1].[DownloadId] AS [DownloadId],
[Extent1].[UnlimitedDownloads] AS [UnlimitedDownloads],
[Extent1].[MaxNumberOfDownloads] AS [MaxNumberOfDownloads],
[Extent1].[DownloadExpirationDays] AS [DownloadExpirationDays],
[Extent1].[DownloadActivationTypeId] AS [DownloadActivationTypeId],
[Extent1].[HasSampleDownload] AS [HasSampleDownload],
[Extent1].[SampleDownloadId] AS [SampleDownloadId],
[Extent1].[HasUserAgreement] AS [HasUserAgreement],
[Extent1].[UserAgreementText] AS [UserAgreementText],
[Extent1].[IsRecurring] AS [IsRecurring],
[Extent1].[RecurringCycleLength] AS [RecurringCycleLength],
[Extent1].[RecurringCyclePeriodId] AS [RecurringCyclePeriodId],
[Extent1].[RecurringTotalCycles] AS [RecurringTotalCycles],
[Extent1].[IsRental] AS [IsRental],
[Extent1].[RentalPriceLength] AS [RentalPriceLength],
[Extent1].[RentalPricePeriodId] AS [RentalPricePeriodId],
[Extent1].[IsShipEnabled] AS [IsShipEnabled],
[Extent1].[IsFreeShipping] AS [IsFreeShipping],
[Extent1].[ShipSeparately] AS [ShipSeparately],
[Extent1].[AdditionalShippingCharge] AS [AdditionalShippingCharge],
[Extent1].[DeliveryDateId] AS [DeliveryDateId],
[Extent1].[IsTaxExempt] AS [IsTaxExempt],
[Extent1].[TaxCategoryId] AS [TaxCategoryId],
[Extent1].[IsTelecommunicationsOrBroadcastingOrElectronicServices] AS [IsTelecommunicationsOrBroadcastingOrElectronicServices],
[Extent1].[ManageInventoryMethodId] AS [ManageInventoryMethodId],
[Extent1].[UseMultipleWarehouses] AS [UseMultipleWarehouses],
[Extent1].[WarehouseId] AS [WarehouseId],
[Extent1].[StockQuantity] AS [StockQuantity],
[Extent1].[DisplayStockAvailability] AS [DisplayStockAvailability],
[Extent1].[DisplayStockQuantity] AS [DisplayStockQuantity],
[Extent1].[MinStockQuantity] AS [MinStockQuantity],
[Extent1].[LowStockActivityId] AS [LowStockActivityId],
[Extent1].[NotifyAdminForQuantityBelow] AS [NotifyAdminForQuantityBelow],
[Extent1].[BackorderModeId] AS [BackorderModeId],
[Extent1].[AllowBackInStockSubscriptions] AS [AllowBackInStockSubscriptions],
[Extent1].[OrderMinimumQuantity] AS [OrderMinimumQuantity],
[Extent1].[OrderMaximumQuantity] AS [OrderMaximumQuantity],
[Extent1].[AllowedQuantities] AS [AllowedQuantities],
[Extent1].[AllowAddingOnlyExistingAttributeCombinations] AS [AllowAddingOnlyExistingAttributeCombinations],
[Extent1].[DisableBuyButton] AS [DisableBuyButton],
[Extent1].[DisableWishlistButton] AS [DisableWishlistButton],
[Extent1].[AvailableForPreOrder] AS [AvailableForPreOrder],
[Extent1].[PreOrderAvailabilityStartDateTimeUtc] AS [PreOrderAvailabilityStartDateTimeUtc],
[Extent1].[CallForPrice] AS [CallForPrice],
[Extent1].[Price] AS [Price],
[Extent1].[OldPrice] AS [OldPrice],
[Extent1].[ProductCost] AS [ProductCost],
[Extent1].[SpecialPrice] AS [SpecialPrice],
[Extent1].[SpecialPriceStartDateTimeUtc] AS [SpecialPriceStartDateTimeUtc],
[Extent1].[SpecialPriceEndDateTimeUtc] AS [SpecialPriceEndDateTimeUtc],
[Extent1].[CustomerEntersPrice] AS [CustomerEntersPrice],
[Extent1].[MinimumCustomerEnteredPrice] AS [MinimumCustomerEnteredPrice],
[Extent1].[MaximumCustomerEnteredPrice] AS [MaximumCustomerEnteredPrice],
[Extent1].[HasTierPrices] AS [HasTierPrices],
[Extent1].[HasDiscountsApplied] AS [HasDiscountsApplied],
[Extent1].[Weight] AS [Weight],
[Extent1].[Length] AS [Length],
[Extent1].[Width] AS [Width],
[Extent1].[Height] AS [Height],
[Extent1].[AvailableStartDateTimeUtc] AS [AvailableStartDateTimeUtc],
[Extent1].[AvailableEndDateTimeUtc] AS [AvailableEndDateTimeUtc],
[Extent1].[DisplayOrder] AS [DisplayOrder],
[Extent1].[Published] AS [Published],
[Extent1].[Deleted] AS [Deleted],
[Extent1].[CreatedOnUtc] AS [CreatedOnUtc],
[Extent1].[UpdatedOnUtc] AS [UpdatedOnUtc]
FROM [dbo].[Product] AS [Extent1]
WHERE [Extent1].[Deleted] <> cast(1 as bit)
ORDER BY [Extent1].[CreatedOnUtc] DESC
结语

Linq技术、扩展方法、Lambd表达式是关键点,扩展方法请参考HtmlHelper扩展方法视图页加载问题 ,我们掌握这几个C#的特性,Linq技术的掌握就易如反掌。

本站文章除注明转载外,均为本站原创或翻译,欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,共创和谐网络环境。
转载请注明:文章转载自:华晨软件-云微开发平台 » 初识Web框架-Linq技术有那么深奥吗
本文标题:初识Web框架--Linq技术有那么深奥吗
本文地址:https://www.hocode.com/OrgTec/back/0006.html

相关文章: web框架UI系列--216 Web安全色 ¦

电话
电话 18718672256

微信
二维码