Reputation: 1065
Imagine my LINQ to SQL query is this:
var query = (from q in db.GetTable<potato>()
where q.ID == dbID
select q).FirstOrDefault();
How would I iterate horizontally instead of vertically?. So there's just the one row, I want to iterate through each data item in a column per column basis, instead of row by row. There's quite a few properties so I'd just like to iterate instead of writing them all manually.
Upvotes: 0
Views: 1190
Reputation: 4907
If data you want is Property:
var values = typeof(potato)
.GetProperties()
.Select(p=>p.GetValue(query,null))
.ToArray();
If data is Field:
var values = typeof(potato)
.GetFields()
.Select(p=>p.GetValue(query))
.ToArray();
If some property must be returned you can filter PropertyInfoes or FieldInfoes like below:
typeof(potato)
.GetFields()
.Where(p=>...labla...)
.Select...
Upvotes: 1
Reputation: 2866
You can get this through reflection
foreach (PropertyInfo propertyInfo in potato.GetType().GetProperties())
{
if (propertyInfo.CanRead)
{
string val= propertyInfo.GetValue(potato, null).ToString();
}
}
Upvotes: 1