Peter
Peter

Reputation: 761

Return List<T> via WCF service

I'm writing WP 8 application where I need to return list of users via WCF service. I have two tables in the database: Users (contains location and other user's data) and Tabs (contains two fields u1ID(my Id) and u2ID(user I can see on the map). Thus I get all u2ID and compare them to Id field of the Users table. If they are similar - I add this user to the list. But getAllusrs method returns only first record from the database. How can I correct it?

My service implementation:

public List<Point> getAllusrs() 
        {
            DataClasses1DataContext data = new DataClasses1DataContext();
            var a = (from s in data.Tabs where s.u1ID == 1 select s.u2ID).ToArray();
            int inc = 0;
                List<Point> location = new List<Point>();

                foreach (var d in data.Users)
                {
                    if (a[inc] != null && d.Id == a[inc]) 
                    {
                        location.Add(new Point() { Lat = d.usrLat, Lon = d.usrLong });
                        inc++;
                    }
                }
                return location;
        } 

Upvotes: 0

Views: 299

Answers (1)

nvoigt
nvoigt

Reputation: 77285

You need to work on your basic loops.

public List<Point> GetAllUsers() 
{
    var data = new DataClasses1DataContext();

    var userIds = data.Tabs.Where(t => t.u1ID == 1).Select(t => t.u2ID).ToList();

    var users = data.Users.Where(u => userIds.Contains(u.Id)).ToList();

    var locations = users.Select(u => new Point { Lat = u.usrLat, Lon = u.usrLong }).ToList();

    return locations;
} 

You should probably do all this stuff on the database, not materializing it in between like I did.

Upvotes: 1

Related Questions