简体   繁体   English

Linq:从数据表中选择行

[英]Linq : select rows from DataTable

I have a DataTable that contain 3000 records.I want to do the replication between Android SQLite & MS SQL. 我有一个包含3000条记录的数据表。我想在Android SQLite和MS SQL之间进行复制。 I want to convert Dataset to JSON. 我想将数据集转换为JSON。 I can't pass that amount of data.So i want to take 500 records 1st time 2nd time next 500 records.Likewise want to do. 我无法传递这么多的数据,所以我想第一次记录500条记录,第二次记录下500条记录。 So I used Linq for select the records from DataTable. 因此,我使用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;
    }

Please help me , this gives an error The Source contain no data source. When Go to CopyToDataTable 请帮助我,这将导致错误The Source contain no data source. When Go to CopyToDataTable The Source contain no data source. When Go to CopyToDataTable line. The Source contain no data source. When Go to CopyToDataTable行时。

I believe your error is actually 我相信您的错误实际上是

The source contains no DataRows. 源不包含DataRows。

As @Pranay Rana mentions, your query is not actually executed until you call CopyToDataTable , but by that time the table is empty: 正如@Pranay Rana提到的那样,直到调用CopyToDataTable ,您的查询才会真正执行,但是到那时表是空的:

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

If you remove the call to Rows.Clear() , your CopyToDataTable() should run. 如果删除对Rows.Clear()的调用,则应运行CopyToDataTable()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM