ASP.NET – How can i create List with anonymous types

I want to search for users in my db and create new types in the WHERE statement.
But when i do this i cant add the Range in my List for the return.

I get the error in “AllUsers.AddRange(user);”

        var AllUsers = new List<string>();
        var url = "https://localhost:44356/";

        string[] namelist = name.Split(" ");

        foreach (var n in namelist)
        {
            var user = await context.Users.Where(r => r.FirstName.ToLower().Contains(n) || r.LastName.ToLower().Contains(n)).Select(
                u => new
                {
                    id = u.Id,
                    Name = u.FirstName + u.LastName,
                    Beschreibung = u.Description,
                    Avatar = url + u.ProfileImagePath.Remove(0, 58),
                    Header = url + u.HeaderImagePath.Remove(0, 58),

                }).ToListAsync();

            AllUsers.AddRange(user);
            
            
        }

        var mitglieder = AllUsers.Distinct().ToList();

        return Ok(mitglieder);

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

That because your entity user is an object and you are trying to store it in a list of strings.
You can create a new class UserDto, and store a list of UserDto, instead of a List because chances are that you will end up referencing some property of
that anonymous object later, userDto could be useful on that

//var AllUsers = new List<**string**>(); not this

var AllUsers = new List<UserDto>(); //this 

public class UserDto
{
    public int Id {get; set;}
    public string Name {get; set;}
    public string Beschreibung {get; set;}
    public string Avatar {get; set;}
    public string Header {get; set;}
}

var AllUsers = new List<object>(); //or this

Method 2

Your variable AllUsers is an List<string> type and user is an anonymous object type which is impossible to cast to string.

If you want to use anonymous type for it, you might do this:

var AllUsers = Enumerable.Empty<object>().Select(obj => new
{
    Id = default(int),
    Name = default(string),
    Beschreibung = default(string),
    Avatar = default(string),
    Header = default(string)
}).ToList();

Method 3

There could be several solutions to this problem but I would recommend using this one:

Create a class with all the fields that you require from the Select expression

public class UserViewModel
{
    public int Id {get; set;}
    public string Name {get; set;}
    public string Beschreibung {get; set;}
    public string Avatar {get; set;}
    public string Header {get; set;}
}

Then modify your code like this:

var AllUsers = new List<UserViewModel>();
var url = "https://localhost:44356/";

string[] namelist = name.Split(" ");

foreach (var n in namelist)
{
    var user = await context.Users.Where(r => r.FirstName.ToLower().Contains(n) || r.LastName.ToLower().Contains(n))
           .Select(u => new UserViewModel
            {
                Id = u.Id, // if you have a string, then modify the model to support string values
                Name = u.FirstName + " " + u.LastName, // Added a space in first and last name
                Beschreibung = u.Description,
                Avatar = url + u.ProfileImagePath.Remove(0, 58),
                Header = url + u.HeaderImagePath.Remove(0, 58),
            }).ToListAsync();
    AllUsers.AddRange(user);
}

var mitglieder = AllUsers.GroupBy(g => g.Id).Distinct().ToList(); // You need to group the items so you can get the distinct items on the basis of ID.

return Ok(mitglieder);

Notice, in the Distinct() method, we have grouped the elements so you can actually get the distinct elements.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x