0

I don't really know if this makes any sense but I'm gonna give it a try cause I'm not getting anywhere on my own and I need to learn.

I have this code:

List<User> topuser = db.Database.SqlQuery<User>("SELECT TOP 5 * FROM Users ORDER BY DimensaoRede DESC").ToList();

And I have a method that converts objects of the type "User" to UserModel like this:

I loop through the list and convert the users in it

public static UserModel UserToUserModel(User user){
    UserModel model = new UserModel
    {
        id = user.UserId,
        AdvanceLevel = user.AdvanceLevel,
        DimensaoRede = user.DimensaoRede,
        FortalezaRede = user.FortalezaRede,
        NormalLevel= user.NormalLevel,
        UserName = user.UserName
    };

    if (user.Facebook!=null) model.Facebook= user.Facebook;
    if (user.LinkedDin != null) model.LinkedDin = user.LinkedDin;
    if (user.Status != null) model.Status = user.Status.Descrição;
    if (user.Nome != null) model.Nome = user.Nome;
    model.Tags = user.Tags.Select(s => s.Nome).ToList();

    return model;
}

But an User when he is created the Tag list "Tags" is null so when he queries the Database the List Tags comes as Null.

So when the application runs this:

model.Tags = user.Tags.Select(s => s.Nome).ToList();

It breaks because it is null. How can i solve this problem?

0

1 Answer 1

1

Yes, you can solve it with using LINQ instead of Sql query.

var users = db.Users.Include(u => u.Tags)
                     .OrderByDescending(u => u.DimensaoRede)
                     .Take(5);

Your User class should have navigation property named Tags to do this.It is the cleanest and easiest way.And with LINQ you don't need another method to convert your Users, you can do the following:

var userModels = var users = db.Users.Include(u => u.Tags)
                     .OrderByDescending(u => u.DimensaoRede)
                     .Take(5)
                     .Select(u => new UserModel {
                                  id = u.UserId,
                                  AdvanceLevel = u.AdvanceLevel,
                                  DimensaoRede = u.DimensaoRede,
                                  FortalezaRede = u.FortalezaRede,
                                  NormalLevel= u.NormalLevel,
                                  UserName = u.UserName,
                                  Facebook = u.Facebook != null ? u.Facebook : null,
                                  LinkedDin = u.LinkedDin != null ? u.LinkedDin : null,
                                  Status = u.Status  != null ? u.Status  : null,
                                  Nome = u.Nome != null ? u.Nome : null,
                                  Tags = u.Tags.Select(s => s.Nome).ToList()
                               }).ToList();
Sign up to request clarification or add additional context in comments.

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.