0

我一直在尝试用 LINQ 结果填充数据表,这样我就可以遍历 DT 并打印出所需的数据,但无论我如何尝试,它都无法将匿名类型转换为数据表。

我正在使用的 LINQ 是:

        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();
        }

有什么方法可以将结果放入数据表或任何其他对象中,以便我可以逐行处理数据?

4

1 回答 1

1

DataTable如果您只想遍历 linq 查询的结果,则不必创建

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;
}
于 2013-03-30T18:47:05.627 回答