Hello people, I hope everyone is well.
I have just started learning silverlight and retrieving data through Linq to SQL.
I have a problem with retrieving certain coulums from the table. I can retrieve through an object that retrieves all the columns but I don't want this. i managed to get a string of the columns I want but I cant reference the method from the client.....TIA.
Here is my code:

public class Service1 : IService1
    {
        public List<Contact> getNames()
        {

            DataClassesDataContext db = new DataClassesDataContext();
            var names = from persons in db.Contacts
                        where persons.FirstName == "Gerhard"
                        select persons;

                         
                
            return names.ToList();
        }
    }

This works fine but I dont want every column.

my code that get a single column:

public List<Contact> getNames()
        {

            DataClassesDataContext db = new DataClassesDataContext();

            var result = (from persons in db.Contacts
                       
                          select
                            persons.FirstName).ToList();

                                   
            return result.ToList();
        }

and on the front end

public MainPage()
        {
            InitializeComponent();
           LoadData();

        }
        private void LoadData()
        {
            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            client.getNamesCompleted += new EventHandler<ServiceReference1.getNamesCompletedEventArgs>
            (client_getNames);
            client.getNamesAsync();

        }

        private void client_getNames(object sender, ServiceReference1.getNamesCompletedEventArgs e)
        {
            dataGrid1.ItemsSource = e.Result;
        }

I really need someone to show me how to call the method that returns a string list

You need to change your method signature from List<Contact> to List<FirstNameType>

So if your FirstName data type is a String, it would be List<String> Personally, unless you want to modify the list (which in this case would be bad) use should return IEnumerable<String> I suggest you look up how to use Generics (this is the List and IEnumerable) as they allow you to return any type so long as you tell it what this type is. This is why you specify the class name in the < >

So to return a list of ints: List<int> :)


What your first LINQ2SQL was doing, is returning a list of "Contact" class types, because they were the objects you were searching for.

However, in your second statement, you're only asking it for a list of strings from that Contact, so you cannot return a list of Contacts, because they were never created.

If you want to return a list of Contacts with only the first name filled in:

foreach(String firstName in result.ToList())
{
   yield return new Contact { FirstName = firstName };
}

I really need someone to show me how to call the method that returns a string list

To get a list of strings, change this:

public List<Contact> getNames()

to

public List<string> getNames()

...or am I missing the point?

but you don't need to call ToList() twice

public List<string> getNames()
        {
            DataClassesDataContext db = new DataClassesDataContext();
            return db.Contacts.Select(p => p.firstName).ToList(); // or yield return
        }

Edited 4 Years Ago by thines01: n/a

Thanx thines and ket, I am now trying to get more than two columns i.e the name and surname...this is what I have resorted to

public List<String> getFirstNames()
        {

            DataClassesDataContext db = new DataClassesDataContext();

            var result = (from persons in db.Contacts

                          select
                            persons.FirstName +", "+ persons.LastName).ToList();

            return result.ToList();
        }

But this just returns them as one string...When I use generic list like below I get an error:

public IEnumerable<String> getFirstNames()
        {

            DataClassesDataContext db = new DataClassesDataContext();

            var result = (from persons in db.Contacts

                          select new{
                            persons.FirstName ,persons.LastName}).ToList();

            return result.ToList();
        }

It says it cant convert anonymous type to string.

What you have done is created an anonymous type. This is a type that you have created a runtime that there is no definition for.

In this case your anonymous type is { String, String }

This is equivalent to having a class of

public class MyDoubleString
{
    String string1 = "";
    String string2 = "";
}

Unfortunately, this is going to be more difficult than you think and it would probably be better to return them as a single string with a delimiter.

If you really *must* return two seperate strings in a list. Declare a custom type for this purpose.

Or if you *really* want to complicate things and you can use .NET 4, take a look at the dynamic type. But I advise against it, as I'm a big fan of strongly typed programming (yes I hate "var" as well ;) )

Ohk I get you buddy, so you think its better I create a property? There are properties that are auto generated, do I put it in the same file? (I am new to C# btw)

This article has been dead for over six months. Start a new discussion instead.