Get the App
SLTechnology News&Howtos  ›  Development  › 

ADO.NET object query how to use

Shulou Source: shulou.com Published: 2022-06-02 08:17:59 11月08日 Update

This article mainly introduces "how to use ADO.NET object query". In daily operation, I believe many people have doubts about how to use ADO.NET object query. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use ADO.NET object query". Next, please follow the editor to study!

The ObjectQuery generic class represents a query that returns a collection of zero or more typed entity objects. Object queries always belong to the existing object context. This context provides the connection and metadata information needed to write and execute queries.

A typed ObjectContext contains a set of properties that return a typed ObjectQuery instance. Each entity type in the model has one of these attributes. These properties make it easier to create instances of typed ObjectQuery. An object query is executed in the following scenario: when an operation is performed on it, for example, during the foreach (C #) or For Each (Visual Basic) enumeration. When allocated to populate the List collection. When the Execute method is explicitly called. When a LINQ query execution operator, such as First or Any, is called.

The following query returns a Contact object whose first and last name is specified by the passed parameter:

'Get the contacts with the specified name. Dim contactQuery As ObjectQuery (Of Contact) = _ context.Contact _ .Where ("it.LastName = @ ln AND it.FirstName = @ fn", _ New ObjectParameter ("ln", lastName), _ New ObjectParameter ("fn", firstName))

For a complete example of how to write and execute a query using an object context, see how to: execute a query that returns an entity type (entity framework). For more information about Entity SQL queries, see the Entity SQL language.

Query projection

When ADO.NET object queries are used to return entity data model (EDM) data as entity objects, ADO.NET object queries also support projection, so it is difficult to materialize the data returned by projection into entity types. ObjectQuery uses the DbDataRecord type for projections that return non-entity types, which can be nested results or anonymous types. Simple types, such as Int32 or String, are used with projections that return a single property value.

The following considerations apply to query projection: ObjectQuery can be initialized to represent a single scalar result rather than a scalar result collection. Some extension methods require the result of the collection as input. In this case, an ArgumentException is thrown when one of the methods is called, as shown in the following example.

'Define a query projection that returns'a single scalar value rather than a collection. Dim scalarQuery As ObjectQuery (Of Int32) = _ New ObjectQuery (Of Int32) ("100s", advWorksContext) 'Calling an extension method that requires a collection' will result in an exception. Dim hasValues As Boolean = scalarQuery.Any () at this point, the study on "how to use ADO.NET object query" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Queries objects types entities projections properties data methods results learning up and down context more information individual instance scalar model example face Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Linux MySQL Apple