0

I have a stored procedure with a left join that returns something like this:

name1  car1
name1  car2
name1  car3
name2  car1
name3  null
name4  null

I need to store this result in an object like this:

class Person 
{
    string Name;
    List<string> Cars;
}

What is the best way to do this? Either directly in the stored procedure or in code after retrieving the query result.

That I do now is once I have the result from db to iterate over it and add the name and its list of cars c#to another object if name is not already there.

Is there a better solution to this?

2
  • What data access technology are you using? Can you provide some code of how you execute the stored procedure? Sep 10, 2015 at 20:39
  • I am using Dapper, calling it like this var queryResult = PersonRepo.ExecuteQuery<PersonInfo>("dbo.GetPersonsCars", null, CommandType.StoredProcedure).ToList(). PersonInfo class is like Person class listed above but Cars is not a list, but simple a string. So I have to create 2 classes..
    – Andrea
    Sep 10, 2015 at 20:48

3 Answers 3

0

Assuming you have a DataReader returning the join results I'd do this:

        Dictionary<string, List<string>> people = new Dictionary<string, List<string>>();
        while (reader.Read())
        {
            string name = reader.GetString(0);
            string car = reader.GetString(1);
            List<string> cars = null;
            if (!people.TryGetValue(name, out cars))
            {
                people[name] = cars = new List<string>();
            }
            if (null != car)
                cars.Add(car);
        }

Each dictionary key/value pair represents a Person. But you could create instances of Person now from the dictionary entries if you still need them. I would just use the dictionary to access the people and their cars.

0

I would write a more reusable DataAccess class that returns an IEnumerable<T>, always. Depending on your requirements, you might/might not want to call .ToList() to force enumeration.

Then you can do whatever you want, like converting the IEnumerable<T> to whatever you need like IDictionary<string, List<string>>, or perhaps use the .GroupBy() LINQ method which may satisfy your requirements also.

You've got options, that's for sure.

0
0

Suppose you have two tables in your database. Person and Cars. For simplicity assume Person to have two columns PersonID, Name, while Cars has three columns CarID, Name, PersonID. The field PersonID in the Cars table is the foreing key that links a car to a person.

A simple query that associates your Person to your Car is

string query = @"
SELECT p.PersonID, p.Name, c.CarID, c.Name 
FROM Person p LEFT JOIN Cars c on p.PersonID = c.PersonID";

and this could be your Model

public class Person
{
    public int PersonID {get;set;}
    public string Name { get; set; }
    public List<Car> Cars {get;set;}
}
public class Car
{
    public int CarID { get; set; }
    public string Name { get; set; }
    public int PersonID {get;set;}
}

Now you could write this to work with Dapper

var lookup = new Dictionary<int, Person>();
using (IDbConnection cnn = GetOpenConnection())
{
    var people = cnn.Query <Person, Car, Person>(query, (per, car) =>
    {
        Person found;
        if (!lookup.TryGetValue(per.PersonID, out found))
        {
            found = per;
            lookup.Add(per.PersonID, found);
            found.Cars = new List<Car>();
        }
        found.Cars.Add(car);
        return found;
    }, splitOn: "CarID").Distinct();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.