How do I use anonymous type in LINQ?
Category: System.Linq, viewed: 71 time(s).
In this example we use an anonymous type as the return of our LINQ query execution. Using the anonymous type gives LINQ a powerful flexibility because we don't need to create a specific object for each type of result we want out query to return.
In the example below we query the Book data source. The Book class only have two properties the Title and the Price. But in the query we also want to calculate the tax value and the total price of the book after tax. As you can see that we also can do a calculation inside the LINQ query.
Here is the output of our program:
Title : Windows Forms in Action Net Price : 50.00 Tax : 5.00 Total Price: 55.00 =================================================== Title : Programming C# Net Price : 35.99 Tax : 3.60 Total Price: 39.59 =================================================== Title : .NET Framework Essentials Net Price : 49.99 Tax : 5.00 Total Price: 54.99 =================================================== Title : Professional Crystal Reports Net Price : 45.00 Tax : 4.50 Total Price: 49.50 ===================================================
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!
Related Examples
|
|