Kelly Robins
Kelly Robins

Reputation: 7313

Passing LINQ Results to a function

I have a class called UserInfo that contains details about a given user.

There are several places in code where the data might be queried and I'd like to have a single function to fill the UserInfo object with the corresponding data from the Linq Query.

                var userData = dc.Users.Where(λ => (λ.Login == username) && λ.Active)
                  .Select(λ => new { λ.ID, Salt = λ.Seasonings.Single().Salt, λ.Login, λ.PassHash, λ.Admin, λ.Trusted, λ.E_mail, λ.Name, λ.Phone, λ.Note, λ.RegistrationDate }).SingleOrDefault(); 
                string tmppass = generatePassHash(password, userData.Salt);
                if (userData.PassHash.Trim() == tmppass.Trim())
                {
                    ID = userData.ID;
                    // Here is the stuff i'd like to move to a function
                    _user._id = userData.ID;
                    _user._userState = State.NotAuthorized;
                    _user._username = userData.Login;
                    _user._name = userData.Name;
                    _user._email = userData.E_mail;
                    _user._phone = userData.Phone;
                    _user._notes = userData.Note;
                    ...

                }

How do I properly set up a function to accept this anonymous type as an argument? Do I need to declare a new interface or is there a simpler way?

Thanks for the help!

PS- sorry for the excessive underscores, nested classes make things a bit messy.

Upvotes: 2

Views: 2192

Answers (2)

Rex M
Rex M

Reputation: 144182

For simplicity's sake, couldn't you just have all your routines accept the entity object itself? E.g. if dc.Users is a table of type UserEntity, skip the Select():

UserEntity userData = dc.Users.Where(
       λ => (λ.Login == username) && λ.Active).SingleOrDefault();

And if that isn't acceptable, encapsulate a more limited object which takes UserEntity as a ctor parameter:

public class UserInfo
{
    public string Name {get;set;}
    public string Phone {get;set;}
    ...

    public UserInfo(UserEntity entity)
    {
        this.Name = entity.Name;
        this.Phone = entity.Phone;
        ...
    }
}

var userData = dc.Users.Where(
       λ => (λ.Login == username) && λ.Active).Select(
         λ => new UserInfo(λ)).SingleOrDefault();

This abstracts the messy conversion away from the rest of your application. However, in general I'd recommend simply using the entity object, since it makes it much easier to go in reverse when you need (passing a modified entity back to the DAL).

Upvotes: 2

Beatles1692
Beatles1692

Reputation: 5330

I'm afraid It's not possible to pass an anonymous type as an argument to another method. But I wonder why you are using an anonymous type and not working with user in the first place?

PS : BTW if you are applying the same pattern through out your code why don't you implement a concrete class for UserInfo ?

Upvotes: 2

Related Questions