Question : Linq to SQL iEnumerable Select Statements

I am trying to use an iEnumerable statement in a Linq Data Source. I can get the iEnumerable to return all of the columns of a table using, "Dim result As IEnumerable(Of CandidateAlert) = query.Select(Function(ca As CandidateAlert) ca)"... but I don't know how to specify individual colums and columns from other tables linked through a foreign key.

Any suggestions?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
Protected Sub ldsAlert_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles ldsAlert.Selecting
        Dim db As New ASCDataContext
        Dim query As IQueryable(Of CandidateAlert) = db.CandidateAlerts
        query = query.Where(Function(ca As CandidateAlert) ca.Candidate.SpecialtyID <> 364)
        query = query.OrderBy(Function(ca As CandidateAlert) ca.CreatedDate)
        Dim result As IEnumerable(Of CandidateAlert) = query.Select(Function(ca As CandidateAlert) ca.Candidate.Specialty.Specialty, ca.CandidateID, ca.City, ca.State, ca.Distance, ca.Ties, ca.CreatedDate, ca.Candidate.WorkStatus.StatusName, ca.Candidate.CandidateAlerts.Count, ca.Candidate.Username)
        e.Result = result
    End Sub

Answer : Linq to SQL iEnumerable Select Statements

Hi bsherp;

To your question, "but I don't know how to specify individual columns and columns from other tables linked through a foreign key."

I will user the Microsoft Northwind demo database with the Orders and Detail_Orders tables to show this:

Dim query As IEnumerable(Of Order) = _
    db.Orders.Select(Function(ord) ord)
   
As you noted in your question the above query will return all the columns of the Orders table. Note that the query returns a IEnumerable(Of Order), Order is the copy of the Orders table in the database. In order to get the Order_Details for each order using the relationship defined in the database you need to use the variable in the Order class Order_Detail which is of type EntitySet. Enumerating over the above query using the EntitySet to get access to the Order_Details is shown below.

For Each o As Order In query
    ' This data comes from the Orders table
    Console.WriteLine(o.CustomerID & "  :  " & o.OrderID)
    For Each od As Order_Detail In o.Order_Details
        ' Because the Order_Details, EntitySet, is a collection you need to enumerate over
        ' it to get all the records.
        Console.WriteLine(vbTab & od.ProductID & "  :  " & od.UnitPrice & "  :  " & od.Quantity)
    Next
Next

Because Linq by default does deferred loading the initial execution of the query returns only the Orders table data. Each time the inner For Each loop is iterated a new query is sent to SQL server to get the child records. If you want to get all records so that only one call is made to SQL server you add the following just after defining the data context.

Dim db As New NorthwindDataContext()
' The following three line will cause all the data to get retrieved at the same time.
Dim dlopt As New DataLoadOptions()
dlopt.LoadWith(Of Order)(Function(o As Order) o.Order_Details)
db.LoadOptions = dlopt

The code in the code snippet show how to just get individual columns for each table. Note that I created two classes at the end of that code. This is because when you select less columns they are no longer a type of the original object. If you leave out all reference to the class names it will return an anonymous type.


Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
Dim db As New NorthwindDataContext()
Dim dlopt As New DataLoadOptions()
dlopt.LoadWith(Of Order)(Function(o As Order) o.Order_Details)
db.LoadOptions = dlopt

Dim query As IEnumerable(Of ModifiedOrder) = db.Orders.Select(Function(ord) New ModifiedOrder With _
                                 { _
                                    .CustomerID = ord.CustomerID, _
                                    .OrderID = ord.OrderID, _
                                    .OrderDetail = ord.Order_Details.Select(Function(od) New ModifiedOrder_Detail With _
                                                        { _
                                                            .ProdID = od.ProductID, _
                                                            .UnitPrice = od.UnitPrice, _
                                                            .Quantity = od.Quantity _
                                                        }) _
                                 })

For Each rec In query
    Console.WriteLine("CustomerID = {0}  :  OrderID = {1}", rec.CustomerID, rec.OrderID)
    For Each recOrd In rec.OrderDetail
        Console.WriteLine(vbTab & "Product ID = {0}  Unit Price = {1}  Quantity = {2}", recOrd.ProdID, recOrd.UnitPrice, recOrd.Quantity)
    Next
Next
Random Solutions  
 
programming4us programming4us