I have the following array and Linq statements. I can get the field name with the Linq, but how to I retrieve the field value? (in LinqPad)
var source = new[] {
new { FirstName = "Foo", SurName = "Bar", Password = "secret" },
};
var membersToInclude =
source
.First()
.GetType()
.GetProperties()
.Where(x => x.Name != "Password")
.Select(x => x.Name)
.ToArray();
foreach (var m in membersToInclude)
{
m.Dump();
}
from w in Albums.Take(5)
select new
{
w1 = w.AlbumArtUrl.Substring(1,5),
w2 = w.AlbumArtUrl.Length
}
The specified argument value for the function is not valid. [ Argument # = 1,Name of function(if known) = LEN ]
You're selecting just property's name. You need to apply it to an object to get the value. This should give you an idea:
var source = new[] {
new { FirstName = "Foo", SurName = "Bar", Password = "secret" },
};
var membersToInclude =
source
.First()
.GetType()
.GetProperties()
.Where(x => x.Name != "Password")
.Select(x =>
{
var value = x.GetValue(source.First());
return new {x.Name, value};
})
.ToArray();
foreach (var m in membersToInclude)
{
m.Dump();
}