Reputation: 4570
I have this LINQ which does the job I want
var query = context.MasterTemplateOfficeTag
.Join(context.Tag, x => x.TagId, y => y.Id, (x, y) => new { y.Name })
.ToList();
Though my question is I would like the LINQ to return a list<String>
as the Select syntax => new { y.Name })
is of type string
. Therefore if the compiler knows the return type, why I can't use list<String>
?
I would want something like this
List<String> name = context.MasterTemplateOfficeTag
.Join(context.Tag, x => x.TagId, y => y.Id, (x, y) => new { y.Name })
.ToList();
Is this possible to do?
Thanks
Upvotes: 0
Views: 162
Reputation: 186813
Well
new { y.Name }
is an anonymous object with a single string
field (Name
). Drop new {...}
wrapping and return string
:
List<String> name = context
.MasterTemplateOfficeTag
.Join(
context.Tag,
x => x.TagId,
y => y.Id,
(x, y) => y.Name ) // <- Now we return string: y.Name
.ToList();
Upvotes: 6
Reputation: 16846
Instead of returning an anonymous object, just return the string
List<String> name = context.MasterTemplateOfficeTag
.Join(context.Tag, x => x.TagId, y => y.Id, (x, y) => y.Name)
.ToList();
Upvotes: 2
Reputation: 2750
new { y.Name })
creates an anonymous object with a Name
property.
You need to just return y.Name
to be able to use List<string>
Upvotes: 3