简体   繁体   中英

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. The method works perfectly fine but I have dynamic as a return type because the following 2 didn't work: List & IEnumerable. What should be the correct return type be for a LINQ query 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:

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>'. An explicit conversion exists (are you missing a cast?)

Error when using IEnumerable< 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.IEnumerable<CosialAPI.Authorization.Entities.User>'. 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> .

If you want to change the return type to List<User> , add a ToList() as @juharr said in the comments.

Another option is to create a new class with only the properties you want to expose. 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> .

return listOfFollowers.ToList(); // or .AsEnumerable()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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