Linq:从DataTable中选择行

时间:2011-12-06 04:13:01

标签: c# linq datatable

我有一个包含3000条记录的DataTable。我想在Android SQLite和&amp ;;之间进行复制。 MS SQL。我想将数据集转换为JSON。我无法传递那么多的数据。所以我想第一次拿下500条记录第二次接下来的500条记录。就像想做的那样。所以我使用Linq从DataTable中选择记录。

    public String ConverDataSetToJson(DataSet dsDownloadJson,int currentSplit)
    {
        StringBuilder Sb = new StringBuilder();
        int start = 0;
        int end =500;
        int chk = 0;
        string json = "";
        int currentChk = currentSplit;
        int total =0;
        DataTable dt1 = new DataTable();

        if (dsDownloadJson.Tables.Count > 0)
        {
            Sb.Append("{");
            foreach (DataTable dt in dsDownloadJson.Tables)
            {
                DataTable dtDownloadJson = dt;
                total = dtDownloadJson.Rows.Count;
                // If row count less than 500, then take that amount
                if (dtDownloadJson.Rows.Count < 500)
                {
                    end = dtDownloadJson.Rows.Count;
                }

                //number of split data set
                if (chk == 0)
                {
                    if (dtDownloadJson.Rows.Count > 500)
                    {
                        if ((dtDownloadJson.Rows.Count / 500) == 0)
                        {
                            chk = dtDownloadJson.Rows.Count / 500;
                        }
                        else
                        {
                            chk = dtDownloadJson.Rows.Count / 500 + 1;
                        }
                    }
                    else
                    {
                        chk = 1;
                    }
                    currentChk = 1;
                }
                else
                {
                    currentChk = currentChk + 1;
                    start = currentChk * 500;
                    end = start + 500;
                    currentChk = chk;
                }

                var AllProducts = dtDownloadJson.AsEnumerable();
                if (AllProducts.Count() > 0)
                {
                    var query1 = (from c in AllProducts select c).Skip(0);

                    query1 = (from c in query1 select c).Take((end - start) + 1);
                    int res = query1.Count();
                    Console.WriteLine("---------" + res);
                    if (query1.Count() > 0)
                    {
                        dtDownloadJson.Rows.Clear();
                        dt1 = query1.CopyToDataTable();
                        int count = dt1.Rows.Count;
                        json = JsonConvert.SerializeObject(dt1, Formatting.Indented);
                    }
                }                    
            }
        }

        return json;
    }

请帮助我,这会给出错误The Source contain no data source. When Go to CopyToDataTable行。

1 个答案:

答案 0 :(得分:1)

我相信你的错误实际上是

  

源不包含DataRows。

正如@Pranay Rana所提到的那样,在您调用CopyToDataTable之前,您的查询实际上并未执行,但到那时该表为空:

dtDownloadJson.Rows.Clear(); 
dt1 = query1.CopyToDataTable();

如果您取消对Rows.Clear()的来电,则CopyToDataTable()应该会运行。