简体   繁体   中英

Storing data retrieved from web service reference as array/arraylist

I am now doing a silverlight application, language is C# and I have successfully retrieved the data from web service reference and is able to display it on the mainpage.xaml in grid view. However is there any way for me to store my data as array/arraylist so that instead of displaying all the column data in the grid view, I only want it to display only one column of data. Any experts that can help me out on this? Below is my current basic code

  [OperationContract]
  public List<location> Getlocations()
  {
     DataClassesDBDataContext db = new DataClassesDBDataContext();

     var mlocations = from location in db.locations
                      select location;
     return mlocations.ToList();
  }

if you are using wcf service you have a ability to change the return type.

In Add Service Reference click on the "Advanced" button at the bottom. A new modal dialog box opens "Service reference settings" there you can select "Collection Type" as Arraylist/Array/List.

Now whenever you return List from web method you will receive the one which you have selected in "Collection Type".(This is only available in wcf web service (.svc) and not (.asmx)

If that is not a option then you can just change the list to array.

var list = new List<string>{"1","2"}
string[] arrayVal =" list.ToArray();

As the question is not very clear and you say you only want to show one column in the grid you can just return one column data while returning from the web methods like

 [OperationContract]
 public List<location> Getlocations()
 {
   var db = new DataClassesDBDataContext();

   var mlocations = (from x in db.locations
                    select new location
                    {
                       locationColName = x.something
                    }).ToList();
   return mlocations;
 }

The ToArray() method used on the list should do the trick. A simple general example is shown here:

List<string> l = new List<string>();
l.Add("somedata");
. . .
string[] s = l.ToArray();

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