Connecting Tech Pros Worldwide Help | Site Map

Linq Selection Question.

  #1  
Old August 28th, 2008, 02:05 PM
Kevin Buchan
Guest
 
Posts: n/a
Let's say I have a class, Employee, with three properties: EmployeeID,
Name, Active. I want to find all active employees, and I want to
return a sequence of an anonymous type containing all of the Employee
fields plus one more.

In SQL, I might do this like so:
SELECT E.*, CURRENT_TIMESTAMP AS [Today] FROM Employee AS E

I know I can do something like this in Linq:
'Assume that I have a collection, Employees, holding the data I want
to filter on.
Dim myEmps = From E in Employees Where E.Active = True Select
E.EmployeID, E.Name, E.Active, [Today] = DateTime.Now()

What I'd REALLY like to do is not have to type out all of the property
names (e.g. "E.EmployeID, E.Name, E.Active") just to get them all. I'd
rather do something like the SQL example of "E.*".

Is this possible with Linq?

(Please be patient of some of my code isn't exactly perfect... I typed
this directly into my newsreader.)

Thanks!!

--
Kevin Buchan
kevin.buchan@troutman[nospam]sanders.com
  #2  
Old August 29th, 2008, 01:15 PM
Martin Honnen
Guest
 
Posts: n/a

re: Linq Selection Question.


Kevin Buchan wrote:
Quote:
Let's say I have a class, Employee, with three properties: EmployeeID,
Name, Active. I want to find all active employees, and I want to
return a sequence of an anonymous type containing all of the Employee
fields plus one more.
>
In SQL, I might do this like so:
SELECT E.*, CURRENT_TIMESTAMP AS [Today] FROM Employee AS E
>
I know I can do something like this in Linq:
'Assume that I have a collection, Employees, holding the data I want
to filter on.
Dim myEmps = From E in Employees Where E.Active = True Select
E.EmployeID, E.Name, E.Active, [Today] = DateTime.Now()
>
What I'd REALLY like to do is not have to type out all of the property
names (e.g. "E.EmployeID, E.Name, E.Active") just to get them all. I'd
rather do something like the SQL example of "E.*".
>
Is this possible with Linq?
What you can do is e.g.

Dim myEmps = From E in Employees _
Where E.Active = True _
Select E, [Today] = DateTime.Now()
but then the items returned has two properties, one of type Employees,
one of type DateTime so you would need to access

For Each item in myEmps
Console.WriteLine(item.E.EmployeeID)
Next

I don't think there is a wildcard like E.* possible.





--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linq to Python hrishy answers 21 October 27th, 2008 10:45 PM
linq vs lambda CSharper answers 4 October 1st, 2008 09:05 PM
Datagridview with Linq =?Utf-8?B?VmFuZXNzYQ==?= answers 2 December 11th, 2007 06:05 PM
LINQ Question (Contains) BeSharp answers 4 November 29th, 2007 02:55 AM