简体   繁体   English

不确定linq到sql查询的返回类型

[英]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? 我知道我可能要返回List的东西,但是什么?

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. 我试图在user.UserID ==传递guid参数的用户集上加入一个配置文件表。

Thanks in advance. 提前致谢。

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> 总结您的选项将是: List<MyModel>List<object>List<dynamic>

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. 相反,您应该使用这些属性创建自己的类,然后返回它。

您可以将返回类型设置为动态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM