0

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

2 Answers 2

1

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;
 }
Sign up to request clarification or add additional context in comments.

3 Comments

hi , yes i only wanted to display a column of data and then column name is "latitude". becos i have a converter that would take in values from the database, but i only want it to take it a column of values so i wanted to store as array and then get selected into the converter. i have tired your code but it is giving me this error "error 2Cannot implicitly convert type 'System.Linq.IQueryable<double?>' to 'System.Collections.Generic.IEnumerable<string>'. An explicit conversion exists (are you missing a cast?)" any idea how to solve this?
and may i know what does the locationColName = x.something , someting stands for?
something stands for the database column which will come from db.locations.If location contains latitude column and it is decimal then location class will contain the decimal property called latitude and it should work fine.The error that you are getting is because x.something is decimal and locationColName is string.
0

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.