|
-
Jun 23rd, 2009, 09:36 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] LInq question
Hi there,
I am just starting to use linq, I have created this little bit to pull information out of a datatable.
Code:
var brideFirstName = from r in eventContacts.Tables[0].AsEnumerable() where(r.Field<string>("ContactType") == "bride")
select(r.Field<string>("FirstName"));
BrideFirstNameLabel.Text = brideFirstName.First();
first of all - is that the most efficient way to get a single piece of data from a datatable?
Second question - what if I wanted say three bits of data, like city state and postal code?
I have read several tutorials but am not that familiar with it yet so any human advice would be great!
Thanks...
-
Jun 23rd, 2009, 10:35 PM
#2
Re: LInq question
That looks OK to me, although you really should format the code properly. When people write code sloppily they more easily get confused as to what it does, not to mention that it makes it harder for others to understand. Properly formatting code is easy to do and helps everyone:
Code:
var brideFirstName = from r in eventContacts.Tables[0].AsEnumerable()
where r.Field<string>("ContactType") == "bride"
select r.Field<string>("FirstName");
With regards to the last line, you would call FirstOrDefault if you weren't 100% sure that a matching row will be found. If you are 100% sure then First is the correct option.
With regards to the last question, you would create an anonymous type in the select clause, e.g.
Code:
var brideFullName = (from r in eventContacts.Tables[0].AsEnumerable()
where r.Field<string>("ContactType") == "bride"
select new
{
FirstName = r.Field<string>("FirstName"),
MiddleName = r.Field<string>("MiddleName"),
LastName = r.Field<string>("LastName")
}).First();
The new { ... } part creates a new object with the specified properties with the specified values. It's called an anonymous type because it's not an existing type with a name, hence there's no type name after the 'new' keyword. The type is created ad hoc and only exists for the life of the instance(s) created in that code. You can access the property values just as you would property values of any other object:
csharp Code:
BrideFirstNameLabel.Text = brideFullName.FirstName; BrideMiddleNameLabel.Text = brideFullName.MiddleName; BrideLastNameLabel.Text = brideFullName.LastName;
-
Jun 24th, 2009, 07:48 AM
#3
Thread Starter
Hyperactive Member
Re: LInq question
On your first point about sloppy code - Yeah, I translated that query from one using "uhm, extensions... I think"
With regards to your second question I was just reading O'Riellys .net 3.5 book (the one that is an overview of all it) last night after I posted this and I just read about anonymous types - we'll, a practical use of them.
thanks a lot - that was helpful.
-
Jun 24th, 2009, 08:43 AM
#4
Re: LInq question
 Originally Posted by r0k3t
With regards to your second question I was just reading O'Riellys .net 3.5 book (the one that is an overview of all it) last night after I posted this and I just read about anonymous types - we'll, a practical use of them.
Not only is it a practical use, it's the very reason that anonymous types were created in the first place. While extension methods, lambda expressions and anonymous types all come in handy elsewhere too, they were created specifically to support LINQ.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|