Share via


如何:执行返回匿名类型集合的查询(实体框架)

本主题提供有关如何执行返回匿名类型的实例集的查询的示例。 匿名类型为概念模型中未定义的一种类型。 例如,以下 Entity SQL 查询返回包含带有两列(一个整数类型的 ProductID 和一个字符串类型的 Name)的行的匿名类型集:"SELECT p.ProductID, p.Name FROM AdventureWorksEntities.Products as p"。 以下示例返回 Products 的集合,且 Product 类型在概念模型中定义:"SELECT p FROM AdventureWorksEntities.Products as p"

将使用以下每种实体框架 查询技术演示同一示例:

  • LINQ to Entities

  • Entity SQL 以及 ObjectQuery<T>

  • ObjectQuery<T> 的查询生成器方法

本主题中的示例基于 Adventure Works 销售模型。若要运行本主题中的代码,则必须已经将 Adventure Works 销售模型添加到了您的项目中,并且已经将项目配置为使用实体框架。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)如何:手动配置实体框架项目如何:手动定义实体数据模型(实体框架)

示例

以下是 LINQ to Entities 示例。

Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim query = _
        From product In products _
        Select New With _
        { _
            .ProductId = product.ProductID, _
            .ProductName = product.Name _
        }

    Console.WriteLine("Product Info:")
    For Each productInfo In query
        Console.WriteLine("Product Id: {0} Product name: {1} ", _
                productInfo.ProductId, productInfo.ProductName)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    var query =
        from product in context.Products
        select new
        {
            ProductId = product.ProductID,
            ProductName = product.Name
        };

    Console.WriteLine("Product Info:");
    foreach (var productInfo in query)
    {
        Console.WriteLine("Product Id: {0} Product name: {1} ",
            productInfo.ProductId, productInfo.ProductName);
    }
}

以下是 Entity SQL 示例。

Using context As New AdventureWorksEntities()
    Dim myQuery As String = "SELECT p.ProductID, p.Name FROM AdventureWorksEntities.Products as p"
    For Each rec As DbDataRecord In New ObjectQuery(Of DbDataRecord)(myQuery, context)
        Console.WriteLine("ID {0}; Name {1}", rec(0), rec(1))
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string myQuery = @"SELECT p.ProductID, p.Name FROM 
        AdventureWorksEntities.Products as p";

    foreach (DbDataRecord rec in
        new ObjectQuery<DbDataRecord>(myQuery, context))
    {
        Console.WriteLine("ID {0}; Name {1}", rec[0], rec[1]);
    }
}

以下是查询生成器方法示例。

Using context As New AdventureWorksEntities()
    ' Use the Select method to define the projection. 
    Dim query As ObjectQuery(Of DbDataRecord) = context.Products.Select("it.ProductID, it.Name")

    ' Iterate through the collection of data rows. 
    For Each rec As DbDataRecord In query
        Console.WriteLine("ID {0}; Name {1}", rec(0), rec(1))
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Use the Select method to define the projection.
    ObjectQuery<DbDataRecord> query =
        context.Products.Select("it.ProductID, it.Name");

    // Iterate through the collection of data rows.
    foreach (DbDataRecord rec in query)
    {
        Console.WriteLine("ID {0}; Name {1}", rec[0], rec[1]);
    }
}

另请参见

任务

如何:执行返回实体类型对象的查询(实体框架)
如何:执行返回基元类型集合的查询(实体框架)
如何:执行参数化查询(实体框架)

概念

查询生成器方法(实体框架)