Here's how to get data using c# and Linq

Here's how to get data using c# and Linq

pwc1011pwc1011 Posts: 62Questions: 0Answers: 0
edited January 2010 in General
After pulling out what little hair I have left, I turned to MS Support and the always fantastic, Lingzhi Sun, came up with this...

[code]
public static List ToStringArray(this IQueryable source)
{
Type type = source.ElementType;
var properties = type.GetProperties();
List strs;
List list = new List();
foreach (var i in source)
{
strs = new List();
foreach (var p in properties)
{
object value = p.GetValue(i, null);
strs.Add(value == null ? null : value.ToString());
}
list.Add(strs.ToArray());
}

return list;
}
[/code]

which simplifies the call to your query as this

[code]
var list = db.Table1.Select("new(ID, Name)").ToStringArray();
[/code]

or event easier

[code]
aaData = db.Table1.Select("new(ID, Name)").ToStringArray();
[/code]

I hope this saves others the time I spent,
Patrick

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Nice one Patrick! Thanks for posting this.

    Regards,
    Allan
  • pwc1011pwc1011 Posts: 62Questions: 0Answers: 0
    It's purely selfish... If I save you a little time, you'll be able to make even more upgrades :-)

    Have a great weekend!
This discussion has been closed.