tags:

views:

49

answers:

5

I am trying to get the string value "Admin" from a linq query, but when I do:

string oldModule = dc.Modules
                             .Where(x => x.Id == um.ModuleId)
                             .Select(s => new {s.ModuleName})
                             .FirstOrDefault().ToString();

It returns { ModuleName = Admin } in the oldModule variable instead of just Admin.

+9  A: 

That's because you've introduced an anonymous type into the projection. Try this:

string oldModule = dc.Modules
                     .Where(x => x.Id == um.ModuleId)
                     .Select(s => s.ModuleName)
                     .FirstOrDefault();

I removed the ToString call as well, which would have thrown a NullReferenceException if the Where clause hadn't matched anything.

Jon Skeet
...did it again. :-P
Justin Niessner
Thanks, I totally forgot I could do it with just s => s.ModuleName, for some reason, I have it stuck in my head that when I need to select individual columns and not everything, I have to do s => new {...}
Xaisoft
+2  A: 
       string oldModule = dc.Modules
                         .Where(x => x.Id == um.ModuleId)
                         .Select(s => s.ModuleName)
                         .FirstOrDefault().ToString();
John Saunders
+2  A: 

What about just selecting the ModuleName property?

(from m in dc.Modules
 where m.Id == um.ModuleId
 select m.ModuleName).FirstOrDefault()
Joey
A: 

What is the new doing in

.Select(s => new {s.ModuleName})

I don't know what you're issue is, but I wouldn't be surprised if that's part of it..

I would also maybe just do .Single(s => s.ModuleName) instead of .select and .firstordefault.

Jimmy Hoffa
i'm not 100% familiar with linq 2 sql. I really don't understand the difference between Single, SingleOrDefault, First, FirstOrDefault
Xaisoft
+1  A: 

This happens because new {...} creates a new anonymous type in Select. When you call ToString(), you call it on this anonoymous type, not the ModuleName string. If you just write .Select(s=>s.ModuleName) you will get back a string instead of the anonymous type and oldModule will contain Admin.

In fact, you don't even need ToString() at the end of the query, as the result of FirstOrDefault() is already a string.

Panagiotis Kanavos
After I removed the new and just did s => s.ModuleName, resharper told me ToString() was redundant, lol. Can you explain an anonymous type? Thanks for the short explanation by the way.
Xaisoft
Let me jump ahead, is it because when I do s=> new {s.ModuleName}, it does not know what s.ModuleName's type is or am I looking at this wrong?
Xaisoft
An anonymous type is a type that gets generated by the compiler when it encounters the new{ ...} syntax. It has no specific name, which is why it is called anonymous. The compiler generates the type by creating one property for each item it finds inside the curly braces. In your case the compiler created an anonymous type with a property called ModuleName whose value is Admin. You can find a lot more about them at http://msdn.microsoft.com/en-us/library/bb397696.aspx
Panagiotis Kanavos
So would a better name be Anonymous Name as opposed to Anonymous Type?
Xaisoft