troubleshooting Question

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

Avatar of officedog
officedog asked on
.NET ProgrammingC#
4 Comments1 Solution880 ViewsLast Modified:
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.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros