Link to home
Start Free TrialLog in
Avatar of officedog
officedog

asked on

project linq result(s) into custom class using constructor c#

I hope my terminology is correct as I have only been looking into linq for a short while.

I see some potential benefits into using LINQ, but would like to know if it's possible to do the following.

Currently, we have a Data Access Layer that will for example return a datareader and then call a method to create an instance of a custom type and return it for inclusion into a collection, similiar to this;

             List<User> items = new List<User>();
            try
            {

                using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.CONN_STRING, CommandType.StoredProcedure, "get_users_by_type", null))
                    while (rdr.Read())
                    {
                        items.Add(Generic.PopulateUser(rdr));                    
                }
                return items;
            }

            catch (Exception ex)
            {
                EmailAlert.Alert(ex);
                return null;
            }

This small piece of code an be used from multiple locations to generate a 'User' type.

I am now trying to implement the same in LINQ and do remember seeing an example of it somewhere, but for the life of me, can't find it again.

LINQ code;

            UserContext db = new UserContext();

            List<User> result = (from u in db.users
                                        where u.type == 1
                                        select new User
                                        {
                                            ID = u.id,
                                  Name = u.name
                                        }
                                        ).ToList();
            return result;

Now, while this works, I am hoping to make this a little easier to implement across the whole application in so far as the projection of the result into my custom type. We have various stored procedures that have varying criteria and I really wouldn't want to create the new User() and then set it's properties as above. If the custom type changed, it would be a nightmare

What I would like to be able to do is this;


            UserContext db = new UserContext();

            List<User> result = (from u in db.users
                                        where u.type == 1
                                        select new User(u)
                                        ).ToList();
            return result;

Whereas before, we were speciying each property of the custom type, we would now be passing the 'result' to a constructor within the custom type.

Firstly, not sure if this is written correctly. Secondly, if possible, what does the constructor within the type look like, probably not like this;

Public User(LinqResult result)
{
      this._id = result.id;
}

Hope this all makes sense and there is a solution to my problem.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Have you tried you last example? It should work fine as long as you have a constructor in User which takes in a parameter of whatever type is housed by db.users.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of officedog
officedog

ASKER

Solved
Glad to help  :)