C#LINQ返回结果作为从WCF服务方法的列表,然后用它ASPX Web窗体页 [英] C# return linq result as a list from a wcf service method then use it in aspx web forms page

查看:95
本文介绍了C#LINQ返回结果作为从WCF服务方法的列表,然后用它ASPX Web窗体页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中的新手
我试图用它引用一个数据实体模型WCF服务应用程序从table.Searching S.O.选择一行我找到了一种方法返回LINQ查询结果列表,虽然我还没有找到一种方法尚未使用列表在我的aspx Web窗体page.I不知道如何在aspx页面加载列表,到目前为止我研究在MSDN中并没有帮助我。
我想前preSS我的问题,我可以让你想了解的最佳方式,这是我的code:

I'm a newbie in C# I'm trying to use a wcf service application which references a data entity model to select a row from a table.Searching s.o. I found a way to return linq query results as a list though I haven't found a way yet to use the list in my aspx web forms page.I don't know how to load the list in the aspx page and so far my research in msdn hasn't helped me. I tried to express my problem the best way I could so you would understand, here is my code:

WCF服务应用程序code:

the wcf service application code:

public List<string> getAccountInfo(int uid)
    {

        List<string> result = new List<string>();

        try
        {

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

                var query = from uacc in context.user_accounts
                            where uacc.user_account_id == uid
                            select uacc;

                foreach (var c in query)
                {
                    string row = c.user_account_id + ";" + c.order_id + ";" + c.order_state + ";" + c.estimated_cost + ";" + c.instance_form + ";" + c.time_scedule + ";" + c.invoice + ";" + c.notification + ";" + c.user_account_type + ";" + c.username + ";" + c.password;
                    result.Add(row);
                }

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

在aspx.cs code

the aspx.cs code

protected void Page_Load(object sender, EventArgs e)
    {
        accountInfo_Ref.IaccountInfoSrvcClient accInfoClient = new    accountInfo_Ref.IaccountInfoSrvcClient();

        int id = (int)Session["UserId"];

        List<string> columns = new List<string>(accInfoClient.getAccountInfo(id));

        id_lbl.Text = columns[0];
        order_id_lbl.Text = columns[1];
    }

该服务工作正常。我也愿意听取建议,以更好的方式来做到这一点。

The service works fine. I'm also open to suggestions to better ways to do this.

推荐答案

或者你可以的不可以从服务返回字符串,你可以返回对象的列表。

Or you could not return strings from your service, you could return a list of objects.

如果您必须返回一个字符串必须使用String.Split方法拆分序列化的数据,但是这的确是一个贫穷的方法。
如果必须返回一个字符串,你至少可以使用更好的序列化策略,如JSON或XML。

If you MUST return strings you must split the serialized data using the String.Split method but this is really a poor method. If you MUST return strings you could at least use a better serialization strategy such as JSON or XML.

但真正考虑改变你的服务接口。

But really consider changing your service interface.

现在让我们回到使用结果:

Now let's get back to using your results:


  • 您使用的是服务与该ID
  • 返回记录列表
  • 我假设ID是唯一的,你会得到百达0或1的结果列表中的

  • 每个行添加到列表中包含有关帐户的信息

  • 让每一行都必须劈裂来获取信息

  • you are using a service to return a List of records with that ID
  • i assume that the ID is unique and you will allways get 0 or 1 results in your list
  • each "row" added to the list contains the information about an account
  • so each row must be splitted to get the information

code:

protected void Page_Load(object sender, EventArgs e)
    {
        accountInfo_Ref.IaccountInfoSrvcClient accInfoClient = new    accountInfo_Ref.IaccountInfoSrvcClient();

        int id = (int)Session["UserId"];

        List<string> rows = new List<string>(accInfoClient.getAccountInfo(id));
        // display the first row
        string row = rows.FirstOrDefault();
        if (String.IsNullOrEmpty(row))
        {
           // record cannot be found
        }
        else
        {
            string[] details = row.Split(';');

            id_lbl.Text = details[0];
            order_id_lbl.Text = details[1];
        }
    }

这篇关于C#LINQ返回结果作为从WCF服务方法的列表,然后用它ASPX Web窗体页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆