简体   繁体   中英

Linq : select rows from DataTable

I have a DataTable that contain 3000 records.I want to do the replication between Android SQLite & MS SQL. I want to convert Dataset to 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. So I used Linq for select the records from 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 line.

I believe your error is actually

The source contains no DataRows.

As @Pranay Rana mentions, your query is not actually executed until you call CopyToDataTable , but by that time the table is empty:

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

If you remove the call to Rows.Clear() , your CopyToDataTable() should run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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