Güray Yarar
Güray Yarar

Reputation: 143

Linq Return Type

I wrote this linq query,

var userProfile = (from u in ent.UserProfile
                               join ud in ent.tblUserDetails on u.UserId equals ud.UserId
                               join uc in ent.tblUserContacts on u.UserId equals uc.UserId
                               select new
                               {
                                   u.UserId,
                                   u.UserName,
                                   u.NameSurname,
                                   ud.CityID,
                                   ud.EducationStatusID,
                                   ud.Birthday,
                                   ud.About,
                                   ud.ProfilePicture,
                                   ud.PrestigePoint,
                                   uc.FacebookAccount,
                                   uc.TwitterAccount,
                                   uc.GooglePlusAccount,
                                   uc.LinkedInAccount,
                                   uc.Website
                               }).ToList();
            return userProfile;

And I designed model class also,

public class UserProfileGeneral
{
    public Nullable<int> UserId { get; set; }
    public string About { get; set; }
    public Nullable<DateTime> Birthday { get; set; }
    public Nullable<byte> CityID { get; set; }
    public Nullable<byte> EducationStatusID { get; set; }
    public Nullable<byte> PrestigePoint { get; set; }
    public string ProfilePicture { get; set; }
    public string UserName { get; set; }
    public string NameSurname { get; set; }
    public string FacebookAccount { get; set; }
    public string GooglePlusAccount { get; set; }
    public string LinkedInAccount { get; set; }
    public string TwitterAccount { get; set; }
    public string Website { get; set; }
}

but Visual Studio throws this error:

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'

How can I solve this problem?

Thanks.

Upvotes: 1

Views: 62

Answers (2)

Crono
Crono

Reputation: 10478

You are returning a list of an anonymous type, not your actual type.

Replace your Select New block with this:

Select New UserProfileGeneral {
    UserId = u.UserId,
    UserName = u.UserName,
    // etc...
}

You also could declare a constructor on your UserProfile class that will take three parameters of type of your instances and take care of setting the properties internally.

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160852

You are currently projecting to an anonymous type instead of an instance of UserProfileGeneral.

Instead of

select new {

You want to do

select new UserProfileGeneral()



var userProfile = (from u in ent.UserProfile
                               join ud in ent.tblUserDetails on u.UserId equals ud.UserId
                               join uc in ent.tblUserContacts on u.UserId equals uc.UserId
                               select new UserProfileGeneral()
                               {
                                   UserId  = u.UserId,
                                   UserName = u.UserName,
                                   NameSurname = u.NameSurname,
                                   CityID = ud.CityID,
                                   //and so on
                               }).ToList();

Upvotes: 1

Related Questions