0

I have been trying to fill up a datatable with the LINQ results so I can iterate though the DT and print out the needed data but no matter how I try it cant convert the anonomus type into a datatable.

The LINQ I am using is:

        using (var db = new SiteNavigation())
        {
            dtNavData=  (from n in db.Navigation
                         join st in db.SectionTranslations
                         on n.SectionID equals st.Section.SectionID
                         where n.Category == Category && st.CultureID == CultureID
                         orderby n.Position ascending
                         select new
                         {
                              LinkAddress = st.Section.Type + "/" + st.Section.RouteName,
                              st.Title
                          }).ToList();
        }

Is there some way to get the results into a datatable or any other object so I can step though it row by row and process the data?

  • [see this](http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable) – Manish Mishra Mar 30 '13 at 15:32
  • Is there nothing more effecent? I do not care if the data is in a table or not. I just need to step though the results and assign them to different asp.net objects in the aspx page. –  Mar 30 '13 at 15:52
  • so why cannot you iterate through your list in a foreach loop? – Manish Mishra Mar 30 '13 at 15:56
  • Becasue I get the same can not convert from anonymis error. No matter what I try to full with the LINQ query it can not convert from anonymis –  Mar 30 '13 at 16:42
  • exactly, when you do select in your linq query, it becomes anonymous type. why are you trying to covert it? take the result of your query in var myresult = (from n in db...) and iterate through myresult – Manish Mishra Mar 30 '13 at 17:23
  • Ok how do I iterate though the var myresult? –  Mar 30 '13 at 18:00

1 Answers1

1

you don't have to create a DataTable, if all that you want is to iterate through the result of your linq query

var myResult =  (from n in db.Navigation
                     join st in db.SectionTranslations
                     on n.SectionID equals st.Section.SectionID
                     where n.Category == Category && st.CultureID == CultureID
                     orderby n.Position ascending
                     select new
                     {
                          LinkAddress = st.Section.Type + "/" + st.Section.RouteName,
                          st.Title
                      }).ToList();


foreach(var item in myResult)
{
     string linkAddrs = item.LinkAddress;
     string title = item.Title;
}
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59