LINQ queries return enumerable collections of strongly typed objects. A query can return a collection of anonymous types or named types. You can use the Select clause to return only a single field from the data source. When you do this, the type of the collection returned is the type of that single field. You can also use the Select clause to return multiple fields from the data source.
When you do this, the type of the collection returned is a new anonymous type. You can also match the fields returned by the query to the fields of a specified named type. The following code example shows a query expression that returns a collection of anonymous types that have members populated with data from the selected fields from the data source. LINQ queries can also be used to combine multiple sources of data and return a single result.
This can be done with one or more From clauses, or by using the Join or Group Join query clauses. The following code example shows a query expression that combines customer and order data and returns a collection of anonymous types containing customer and order data. You can use the Group Join clause to create a hierarchical query result that contains a collection of customer objects. Each customer object has a property that contains a collection of all orders for that customer.
The following code example shows a query expression that combines customer and order data as a hierarchical result and returns a collection of anonymous types. The query returns a type that includes a CustomerOrders property that contains a collection of order data for the customer. It also includes an OrderTotal property that contains the sum of the totals for all the orders for that customer.
There are several additional LINQ query operators that you can use to create powerful query expressions. The next section of this topic discusses the various query clauses that you can include in a query expression. For details about Visual Basic query clauses, see Queries. The classes in the System. Linq namespace and the other namespaces that support LINQ queries include methods that you can call to create and refine queries based on the needs of your application.
Visual Basic includes keywords for the following common query clauses. Either a From clause or an Aggregate clause is required to begin a query.
A From clause specifies a source collection and an iteration variable for a query. For example:. A Select clause declares a set of iteration variables for a query. If a Select clause is not specified, the iteration variables for the query consist of the iteration variables specified by the From or Aggregate clause.
A Where clause specifies a filtering condition for a query. An Order By clause specifies the sort order for columns in a query. A Join clause combines two collections into a single collection.
A Group By clause groups the elements of a query result. It can be used to apply aggregate functions to each group. A Group Join clause combines two collections into a single hierarchical collection. Either an Aggregate clause or a From clause is required to begin a query. An Aggregate clause applies one or more aggregate functions to a collection.
For example, you can use the Aggregate clause to calculate a sum for all the elements returned by a query, as the following example does. You can also use the Aggregate clause to modify a query. For example, you can use the Aggregate clause to perform a calculation on a related query collection. A Let clause computes a value and assigns it to a new variable in the query.
A Distinct clause restricts the values of the current iteration variable to eliminate duplicate values in query results. Double-click the Add button to add code to its Click event. Double-click the Update button to add code to its Click event. Double-click the Delete button to add code to its Click event. Press F5 to run your project. Click Add to add a new record. Click Update to modify the new record.
Click Delete to delete the new record. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode.
Is this page helpful? Same error message both ways. Looks impossible with your code. What's the exact exception? And inner exception, if any. Have got any added code in Category itself? Consider adding a new object with new identity and deleting the existing one instead.
I think the BindingSource may cause false assignments. Show 1 more comment. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related
0コメント