你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
你可以使用 LINQ 语法编写针对表服务的查询。 以下示例演示如何编写类似于 查询表和实体中显示的示例查询的示例查询,但使用 LINQ 而不是 REST 协议。
表服务支持执行检索实体的所有属性的简单查询;还可以选择实体属性的子集。 表服务还支持使用 Where 运算符筛选查询结果,并使用 Take 运算符指定要返回的实体数量。
有关表服务支持哪些 LINQ 运算符的详细信息,请参阅 表服务支持的查询运算符。
投影实体属性
LINQ select 子句可用于从一个或多个实体投影属性的子集。 可投影的最大属性数为 255,这也是实体中的最大属性数。
若要投影实体的属性,客户端必须支持 OData 数据服务版本 3.0,通过指定 DataServiceVersion 或 MaxDataServiceVersion 标头来指示,如下所示:
DataServiceVersion: 3.0;NetFx  
MaxDataServiceVersion: 3.0;NetFx  
以下示例演示如何使用所需的对象初始值设定项从单个实体投影属性:
var query = from entity in dataServiceContext.CreateQuery<SampleEntity>(tableName)  
                 where entity.PartitionKey == "MyPartitionKey"  
                 select new { entity.RowKey };  
以下示例从具有 10 个属性的实体投影 3 个属性。 在此示例中,SampleEntity 的 10 个属性为字母 A 到 J:
IEnumerable<SampleEntity> query = from entity in  
                                       dataServiceContext.CreateQuery<SampleEntity>(tableName)  
                                       where entity.PartitionKey == "MyPartitionKey"  
                                       select new SampleEntity  
                                      {  
                                          PartitionKey = entity.PartitionKey,  
                                          RowKey = entity.RowKey,  
                                          A = entity.A,  
                                          D = entity.D,  
                                          I = entity.I  
                                      };  
还可以通过使用标准 REST 请求中的 $select 查询选项来投影实体属性。 有关详细信息,请参阅 查询实体。
有关实体投影和转换的详细信息,请参阅 OData 文档中 ($select) 选择系统查询选项 。
返回前 n 个实体
要返回 n 个实体,请使用 LINQ Take 运算符。 请注意,可在单个查询中返回的最大实体数为 1,000。 为 Take 运算符指定大于 1,000 的值会导致出现错误代码 400(错误请求)。
以下示例从 Customers 表返回前 10 个实体:
var query = (from entity in context.CreateQuery<Customer>("Top10Customers")  
                 select entity).Take(10);  
针对字符串属性进行筛选
以下示例对两个字符串属性进行筛选:
var query = from entity in context.CreateQuery<Customer>("SpecificCustomer")  
                 where entity.LastName.Equals("Smith")  
                 && entity.FirstName.Equals("John")  
                 select entity;  
以下示例使用比较运算符执行前缀匹配以返回 LastName 属性以字母“A”开头的实体:
var query = from entity in context.CreateQuery<Customer>("CustomersA")  
                 where entity.LastName.CompareTo("A") >= 0  
                 && entity.LastName.CompareTo("B") < 0  
                 select entity;  
针对数值属性进行筛选
以下示例返回 Age 属性的值大于 30 的所有实体:
var query = from entity in context.CreateQuery<Customer>("CustomersOver30")  
                 where entity.Age > 30  
                 select entity;  
以下示例返回 AmountDue 属性值小于或等于 100.25 的所有实体:
var query = from entity in context.CreateQuery<Customer>("CustomersAmountDue")  
                 where entity.AmountDue <= 100.25  
                 select entity;  
针对布尔值属性进行筛选
以下示例返回 IsActive 属性设置为 true 的所有实体:
var query = from entity in context.CreateQuery<Customer>("ActiveCustomers")  
                 where entity.IsActive == true  
                 select entity;  
针对日期时间属性进行筛选
以下示例返回 CustomerSince 属性等于 2008 年 7 月 10 日的实体:
DateTime dt = new DateTime(2008, 7, 10);  
var query = from entity in context.CreateQuery<Customer>("CustomerSince")  
                 where entity.CustomerSince.Equals(dt)  
                 select entity;