Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

asp.net - convert linq query results to datatable C#

I want to convert the linq query results to datatable so that I can assign datatable to GridView to show it on asp page.

However I am not able to convert the results to datatable, I am not getting CopyToTable() method in my code.

please advise what am I doing wrong here?

 var gradeData = (from data in oAngieCtxt.prc_ShopInstanceCustomersData(Convert.ToInt32(this.ShopInstanceID), 10000, false)
                     .Where( row => row.RecievedPoints != "n/a" )
                    .GroupBy(row => new { row.Name })
                    .Select(g => new GroupedPoints()
                    {
                        Name = g.Key.Name,
                        TotalPoints = g.Sum(x => Convert.ToDouble(x.RecievedPoints) * (x.Weightage.ToString() == "0.00" ? 1 : Convert.ToDouble(x.Weightage)))
                    })
                     select data).ToList();

 DataTable dt = gradeData --gradeData.CopyToTable()

Note: reference to dataextentions dll is available.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should get DataTableExtensions.CopyToDataTable

Remove ToList().

CopyToDataTable is an IEnumerable<DataRow> extension (unfortunately).

There is a solution with custom CopyToDataTable extension method below.

var gradeData = (from data in oAngieCtxt.prc_ShopInstanceCustomersData(
                 Convert.ToInt32(this.ShopInstanceID), 10000, false)
                .Where( row => row.RecievedPoints != "n/a" )
                .GroupBy(row => new { row.Name })
                .Select(g => new
                {
                    Name = g.Key.Name,
                    TotalPoints = g.Sum(x => Convert.ToDouble(x.RecievedPoints) 
                    * (x.Weightage.ToString() == "0.00" ? 1 
                      : Convert.ToDouble(x.Weightage)))
                })
                 select data);

var dt = gradeData.CopyToDataTable();

Edit:

Here is a more useful implementation of CopyToDataTable There is no type constraint to DataRow.

  public static class DataSetLinqOperators
  {
    public static DataTable CopyToDataTable<T>(this IEnumerable<T> source)
    {
        //you find the ObjectShredder implementation on the blog wich was linked.
        return new ObjectShredder<T>().Shred(source, null, null);
    }

    public static DataTable CopyToDataTable<T>(this IEnumerable<T> source, 
                                     DataTable table, LoadOption? options)
    {
        return new ObjectShredder<T>().Shred(source, table, options);
    }

  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...