Query Result
After a LINQ query is converted to command trees and executed, the query results are usually returned as one of the following:
- A collection of zero or more typed objects or a projection of complex types in the model.
- CLR types.
- Anonymous types.
Anonymous type initialization is shown in the following example in query expression syntax:
using ( FluentModel dbContext = new FluentModel() )
{
var query = from product in dbContext.Products
where product.UnitPrice > 30
select new
{
Name = product.ProductName,
Price = product.UnitPrice
};
foreach ( var item in query )
{
Console.WriteLine( "Name: {0}, Price: {1}", item.Name, item.Price );
}
}
Using dbContext As New FluentModel()
Dim query = From product In dbContext.Products
Where product.UnitPrice > 30
Select New With {Key .Name = product.ProductName, Key .Price = product.UnitPrice}
For Each item In query
Console.WriteLine("Name: {0}, Price: {1}", item.Name, item.Price)
Next item
End Using
User-defined class initialization is also supported. The following example in query expression syntax shows a custom class being initialized in the query:
public class MyProduct
{
public string Name
{
get;
set;
}
public decimal Price
{
get;
set;
}
}
Public Class MyProduct
Public Property Name() As String
Public Property Price() As Decimal
End Class
using ( FluentModel dbContext = new FluentModel() )
{
var query = dbContext.Products.Where( p => p.UnitPrice > 30 ).
Select(p => new MyProduct
{
Name = p.ProductName,
Price = p.UnitPrice
} );
foreach ( var item in query )
{
Console.WriteLine( "Name: {0}, Price: {1}", item.Name, item.Price );
}
}
Using dbContext As New FluentModel()
Dim query = dbContext.Products.Where(Function(p) p.UnitPrice > 30).
Select(Function(p) New MyProduct With {.Name = p.ProductName, .Price = p.UnitPrice})
For Each item In query
Console.WriteLine("Name: {0}, Price: {1}", item.Name, item.Price)
Next item
End Using