簡體   English   中英

C#動態表創建:在wcf服務中使用linq查詢在aspx頁中顯示表數據

[英]C# dynamic table creation : display table data in aspx page using linq query in a wcf service

我是一名C#新手,並且在常規編程中,在上一個問題中, C#從cff服務方法的列表中返回linq結果,然后在aspx Web表單頁面中使用它 ,我設法從表中返回一行並顯示結果我的apx網絡表單頁面中的標簽。 現在,我想顯示整個表->未知數量的行。 我編輯了代碼,幾乎成功了。 問題是我得到的表而不是表包含的四個不同行顯示了前四個 我一遍又一遍地檢查,但找不到錯誤,也找不到一些朋友。 這是我的代碼:

staffPanel.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        displayClients_Ref.IdisplayClientsSrvcClient dcClient = new displayClients_Ref.IdisplayClientsSrvcClient();

        List<string> allClients = new List<string>(dcClient.displayClients());

        foreach (string row in allClients)
        {
            int size = 0;

            string client = allClients.FirstOrDefault();
            if (String.IsNullOrEmpty(client))
            {
                // record cannot be found
            }
            else
            {
                string[] columns = client.Split(';');
                size = columns.Length;


                TableRow tr = new TableRow();
                allClients_tbl.Rows.Add(tr);

                for (int i = 0; i < size; i++)
                {
                    TableCell tc = new TableCell();
                    tc.Text = columns[i];
                    tr.Cells.Add(tc);
                }
            }
        }
    }

displayClient.svc.cs

 public List<string> displayClients()
    {
        List<string> result = new List<string>();
        try
        {

            using (paragon_db_Models.clients_Entity context = new paragon_db_Models.clients_Entity())
            {

                var query = from cl in context.clients
                            select cl;

                foreach (var c in query)
                {
                    string row = c.user_account_id + ";" + c.client_name + ";" + c.client_surname + ";" + c.business_name + ";" + c.client_address + ";" + c.postal_code + ";" + c.telephone_number + ";" + c.fax + ";" + c.email + ";" + c.fiscal_code + ";" + c.public_fiscal_service;
                    result.Add(row);
                }

            }
            return result;
        }
        catch (Exception)
        {
            return result;
        }
    }

如果這是我看不見的簡單,愚蠢的小錯誤,我將刪除問題。 我樂於接受有關其他方法的建議和評論。

你不需要

string client = allClients.FirstOrDefault();

您已經可以通過“行”獲取所需的信息

foreach (string row in allClients)

用“行”替換“客戶端”實例。 FirstOrDefault將始終獲取第一個對象或null。

暫無
暫無

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

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