简体   繁体   中英

C# return linq result as a list from a wcf service method then use it in aspx web forms page

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 so 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:

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;
        }
    }

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.

the method is returning a List<string> .You just have to store it in an instance of List<string> . You have to do like this:

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

UPDATE:

it is returning an array as you said in comments:

string[] columns = accInfoClient.getAccountInfo(id);

or use implict variable:

  var columns = accInfoClient.getAccountInfo(id);

If i understood your question properly Simply write like this

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

You will get list filled with data.

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

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:

  • 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];
        }
    }

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