[英]Unsure on return type of linq to sql query
我有一個方法:
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;
}
我知道我可能要返回List的東西,但是什么?
另外,我可能錯誤地編寫了查詢。 我試圖在user.UserID ==傳遞guid參數的用戶集上加入一個配置文件表。
提前致謝。
您已經創建了一個匿名類型對象,您可以將其作為對象或動態類型返回。 但另一種方法是使用您的必填字段創建一個新類並返回該類型,如:
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;
}
總結您的選項將是: List<MyModel>
, List<object>
, List<dynamic>
這是一個匿名類型。
因為它沒有名稱,所以你無法退貨。
相反,您應該使用這些屬性創建自己的類,然后返回它。
您可以將返回類型設置為動態
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.