[英]Can't find the right return type for a linq query output
I have the following method that returns the output of the LINQ query.我有以下方法返回 LINQ 查询的 output。 The method works perfectly fine but I have dynamic as a return type because the following 2 didn't work: List & IEnumerable.
该方法工作得很好,但我有动态作为返回类型,因为以下 2 不起作用:List & IEnumerable。 What should be the correct return type be for a LINQ query output?
LINQ 查询 output 的正确返回类型应该是什么? My method:
我的方法:
public dynamic GetUserFollowers(Guid user_gd)
{
var listOfFollowers = from users in _context.Users
from userFollowing in _context.UserFollowings.Where(
uf => uf.User2 != users.Gd && uf.User2 == user_gd && uf.User1 == users.Gd
)
select new { users.Gd, users.Firstname, users.Lastname, users.Username, users.Email };
return listOfFollowers;
}
User model:用户 model:
public class User
{
[Key]
public Guid Gd { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public byte[] PasswordHash { get; set; }
public byte[] PasswordSalt { get; set; }
}
Error when using List< User >:使用列表<用户>时出错:
Cannot implicitly convert type 'System.Linq.IQueryable<<anonymous type: System.Guid Gd, string Firstname, string Lastname, string Username, string Email>>' to 'System.Collections.Generic.List<CosialAPI.Authorization.Entities.User>'.
无法将类型“System.Linq.IQueryable<<匿名类型:System.Guid Gd、字符串名字、字符串姓氏、字符串用户名、字符串电子邮件>>”隐式转换为“System.Collections.Generic.List<Cosialities.Authorization。用户>'。 An explicit conversion exists (are you missing a cast?)
存在显式转换(您是否缺少演员表?)
Error when using IEnumerable< User >:使用 IEnumerable<用户> 时出错:
Cannot implicitly convert type 'System.Linq.IQueryable<<anonymous type: System.Guid Gd, string Firstname, string Lastname, string Username, string Email>>' to 'System.Collections.Generic.IEnumerable<CosialAPI.Authorization.Entities.User>'.
无法将类型“System.Linq.IQueryable<<匿名类型:System.Guid Gd、字符串名字、字符串姓氏、字符串用户名、字符串电子邮件>>”隐式转换为“System.Collections.Generic.IEnumerable<CosialAPI.Author.用户>'。 An explicit conversion exists (are you missing a cast?)
存在显式转换(您是否缺少演员表?)
Help would be appreciated!帮助将不胜感激!
Return value for the current code is in the error message:当前代码的返回值在错误消息中:
IQueryable<<anonymous type: System.Guid Gd, string Firstname, string Lastname, string Username, string Email>>
Change this line更改此行
select new { users.Gd, users.Firstname, users.Lastname, users.Username, users.Email };
with this one有了这个
select new User() { Gd = users.Gd, Firstname = users.Firstname, Lastname = users.Lasname, Username = users.Username };
Then you can change dynamic
with IEnumerable<User>
.然后你可以用
IEnumerable<User>
改变dynamic
。
If you want to change the return type to List<User>
, add a ToList()
as @juharr said in the comments.如果要将返回类型更改为
List<User>
,请添加一个ToList()
正如@juharr 在评论中所说的那样。
Another option is to create a new class with only the properties you want to expose.另一种选择是创建一个新的 class 仅包含您要公开的属性。 Eg
例如
public class UserDto
{
public Guid Gd { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Username { get; set; }
public string Email { get; set; }
}
then use it in the query然后在查询中使用它
select new UserDto
{
Gd = users.Gd,
Firstname = users.Firstname,
Lastname = users.Lastname,
Username = users.Username,
Email = users.Email
};
and use either .AsEnumerable()
or .ToList()
to get a return type of IEnumerable<UserDto>
or List<UserDto>
.并使用
.AsEnumerable()
或.ToList()
获取IEnumerable<UserDto>
或List<UserDto>
的返回类型。
return listOfFollowers.ToList(); // or .AsEnumerable()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.