0

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);
1
  • 2
    The problem is, the object user is a dynamic list, which means you cannot add it into a string list. Either, you convert it to JSON to make it string or you make the AllUsers list a dynamic list instead a string based list. Commented Nov 2, 2020 at 0:40

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.