Filling a DataSet or DataTable from a LINQ query result set

后端 未结 7 768
一个人的身影
一个人的身影 2020-12-01 02:38

How do you expose a LINQ query as an ASMX web service? Usually, from the business tier, I can return a typed DataSet or DataTable which can be seri

7条回答
  •  春和景丽
    2020-12-01 03:15

    To perform this query against a DataContext class, you'll need to do the following:

    MyDataContext db = new MyDataContext();
    IEnumerable query = 
        (from order in db.Orders.AsEnumerable()
            select new
            {
                order.Property,
                order.Property2
            })
        as IEnumerable;
    return query.CopyToDataTable();
    

    Without the as IEnumerable; you will see the following compilation error:

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

提交回复
热议问题