views:

2587

answers:

2

I found the following example on http://www.erictobia.com/2009/02/21/LoadADataTableWithLINQ.aspx Unfortunately, I need it in VB and it's using some constructs that neither I nor the automated code converters reciognize. Anyone out there know how this should be written in VB.Net? (the problem spot is the "select new {...")

PeopleDataSet ds = new PeopleDataSet();
using (PeopleDataContext context = new PeopleDataContext())
{
    (from p in context.Persons
     select new
                {
                    Row = ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName)
                }).ToList();

}
+2  A: 

Looks to me to be case of using LINQ for the sake of using LINQ.

Just for each context.Persons

For Each p As Person In context.Persons
   ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName)
Next
AnthonyWJones
Yep. Assuming the code in the question is complete and nothing is ever done with the List<T> created with ToList(), there's no need to use LINQ.
Adam Robinson
Don't declare `p` before the scope of the loop, though. Declare it in the loop head instead (`For Each p [As Person] in context.Persons`).
Konrad Rudolph
Thanks, Konrad I've tweaked the answer, suffice to say I don't spend a great deal of time in VB.NE.
AnthonyWJones
+2  A: 

Anthony has given the correct answer. However, for the record: the new { … } construct can be expressed in VB as follows:

Dim result = From p As Person in context.Persons _
             Select New With { _
                 .Row = ds.Person.AddPersonRow(p.Id, p.FirstName, p.LastName) _
             }
Konrad Rudolph