Léon Zimmermann
Léon Zimmermann

Reputation: 133

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);

Upvotes: 0

Views: 196

Answers (3)

srjheam
srjheam

Reputation: 35

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();

Upvotes: 1

Jamshaid K.
Jamshaid K.

Reputation: 4547

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.

Upvotes: 0

Rod Ram&#237;rez
Rod Ram&#237;rez

Reputation: 1368

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

Upvotes: 1

Related Questions