Converting (Mapping) a List of an entity to another reference explitically

Asked

Viewed 177 times

2

How do I convert/map a list from one entity to another reference explicitly. Here’s an example I was able to perform with an entity:

public static explicit operator UserResponse(Entities.User entity)
{
        return new UserResponse()
        {
            Id = entity.Id,
            Role = entity.Role,
            Email = entity.Email,
            UserStatus = entity.UserStatus
        };
}

So I can convert explicitly within a business rule:

(UserResponse) await _userRepository.Create((User) user);

I would like to do this for a list, rather than using the method below:

public static List<UserResponse> FromIList<User>(IList<Entities.User> entity)
        {
            return entity.Select(item => new UserResponse()
            {
                Id = item.Id,
                Role = item.Role,
                Email = item.Email,
                UserStatus = item.UserStatus
            }).ToList();
        }
    }

2 answers

3


Having explicit conversions, just do the following:

//Convertendo B para A
List<B> listB = listA.Select(a => (B)a).ToList();

//Convertendo A para B
List<A> listA = listB.Select(b => (A)b).ToList();

With the above example I am assuming you have the classes A and B:

public class A
{
    public int Id { get; set; }
    public string Role { get; set; }
    public string Email { get; set; }

    public static explicit operator A(B b)
    {
        return new A
        {
            Id = b.id_b,
            Role = b.prop_b1,
            Email = b.prop_b2
        };
    }
}

public class B
{
    public int id_b { get; set; }
    public string prop_b1 { get; set; }
    public string prop_b2 { get; set; }

    public static explicit operator B(A a)
    {
        return new B
        {
            id_b = a.Id,
            prop_b1 = a.Role,
            prop_b2 = a.Email
        };
    }
}

See the example in fiddle.

  • 1

    I used the code created by you in the example of my answer, I hope it’s not a problem.

  • 2

    @LINQ It’s an honor for me! kkk :)

2

As long as the conversions are valid, you can use the extension method Cast linq.

Considering the code of this answer, would look like this.

var listB = listA.Cast<B>().ToList();
var listA = listB.Cast<A>().ToList();
  • There is something already ready for entities without being in one that has this conversion facility?

  • 1

    I’m sorry, I don’t get it. Not being in a what?

  • I must always make the conversion explicit to accomplish, as the example expressed in the question. I didn’t have to implement with Linq’s Cast. Instead of implementing, there is already some lib that performs this task automatically, rather than implementing?

  • 1

    Do you want to know if there is a library that implements the explicit conversion operator automatically? Type an Automapper only implementing as shown in the Wurthmann response?

  • Show! I came to see this autmapper here... it’s pretty cool! I’ll try to use it. Thanks again!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.