簡體   English   中英

Linq:從數據表中選擇行

[英]Linq : select rows from DataTable

我有一個包含3000條記錄的數據表。我想在Android SQLite和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 The Source contain no data source. When Go to CopyToDataTable行時。

我相信您的錯誤實際上是

源不包含DataRows。

正如@Pranay Rana提到的那樣,直到調用CopyToDataTable ,您的查詢才會真正執行,但是到那時表是空的:

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

如果刪除對Rows.Clear()的調用,則應運行CopyToDataTable()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM