bob
bob

Reputation: 227

Unsure on return type of linq to sql query

I have a method:

public static ???? GetUserProfile(Guid guid) {

            var UserProfileContext = new DataContext.UserProfileDataContext();
            var UserProfile = (from p in UserProfileContext.aspnet_Profiles
                              join u in (from u in UserProfileContext.aspnet_Users 
                                         where u.UserId == guid select u)
                              on p.UserId equals u.UserId
                              select new
                              {
                                  u.UserId,
                                  u.DisplayName,
                                  u.AvatarPath,
                                  u.LastActivityDate,
                                  u.Votes,
                                  u.Cures,
                                  u.LinkTitle,
                                  u.LinkURL,
                                  p.AboutMe
                              }).ToList();
            return UserProfile;
        }

I know that I probably have to return List of something but what?

Also, I may have written the query incorrectly. I am attempting to join one profiles table on the set of users where user.UserID == passed guid parameter.

Thanks in advance.

Upvotes: 2

Views: 102

Answers (3)

VahidNaderi
VahidNaderi

Reputation: 2488

You've created an anonymous type object that you can return it as an object or a dynamic type. But another way can be to create a new class with your required fields and return that type, as in :

public class MyModel
{
    int UserId{ get; set; }
    string DisplayName { get; set; }
    string AvatarPath { get; set; }
    DateTime LastActivityDate { get; set; }
    int Votes { get; set; }
    int Cures { get; set; }
    string LinkTitle { get; set; }
    string LinkURL { get; set; }
    string AboutMe { get; set; }
}
...
public static List<MyModel> GetUserProfile(Guid guid)
{
    var UserProfileContext = new DataContext.UserProfileDataContext();
    var UserProfile = (from p in UserProfileContext.aspnet_Profiles
                          join u in (from u in UserProfileContext.aspnet_Users 
                              where u.UserId == guid select u)
                          on p.UserId equals u.UserId
                          select new MyModel
                          {
                              UserId = u.UserId,
                              DisplayName = u.DisplayName,
                              AvatarPath = u.AvatarPath,
                              LastActivity = u.LastActivityDate,
                              Votes = u.Votes,
                              Cures = u.Cures,
                              LinkTitle = u.LinkTitle,
                              LinkURL = u.LinkURL,
                              AboutMe = p.AboutMe
                          }).ToList();
    return UserProfile;
}

To sum up your options would be: List<MyModel>, List<object>, List<dynamic>

Upvotes: 4

krisdyson
krisdyson

Reputation: 3255

You can set the return type to dynamic

Upvotes: 2

SLaks
SLaks

Reputation: 887285

That's an anonymous type.
Because it has no name, you can't return it.

Instead, you should make your own class with those properties, then return that.

Upvotes: 3

Related Questions