Home > other >  Map List of one Type to a list of another similar Type
Map List of one Type to a list of another similar Type

Time:10-06

I have two different classes:

The following is a model for the database table named "Attachments":

namespace WebApi.Models
{
    public class Attachment
    {
        public enum EntityType
        {
            Transaction
        }
        public int AttachmentId { get; set; }
        public int EntityId { get; set; }
        public EntityType Type { get; set; }
        public string Filename { get; set; }
        public DateTime Created { get; set; }
        public DateTime Updated { get; set; }
    }
}

and in my dbContext class I have something like the following:

namespace WebApi.Models
{
    public class accountingContext : DbContext
    {
        public DbSet<User>? Users { get; set; }
        public DbSet<Transaction>? Transactions { get; set; }
        public DbSet<Attachment>? Attachments { get; set; }

        ///...
    }
}

and then I have a similar cvlass to the Attachement model which is usedf to send data back in response to a web api request, the class is below:

namespace WebApi.Entities.AttachmentResponse;

public class AttachmentResponse
{
    public int AttachmentId { get; set }
    public string Filename { get; set; }
}

I wish to keep my class for responses seperate rather then using the model classes for reasons I don't feel worthwhile going into.

After performing a query against the model classes I need to convert the result and map to the AttachmentResponse class. I am trying the following:

List<WebApi.Entities.AttachmentResponse> attachments = (
    from a in db.Attachments
    from t in db.Transactions.Where(t => a.EntityId == t.TransactionId && t.UserId == userId)
    where a.EntityId == EntityId
    select new
    {
        a.AttachmentId,
        a.Filename
    }).ToList();

But I get an error with the above attempt. I have also looked at ConvertAll but cannot see how to use it in the code above.

Is it possible to accomplish what I am attempting? If so, where am I going wrong above?

CodePudding user response:

You're creating a list of an anonymous type, not a list of AttachmentResponse objects. You need to explicitly create them in the projection:

select new AttachmentResponse
{
    AttachmentId = a.AttachmentId,
    Filename = a.Filename
}

I would also look at tools like AutoMapper that can create mappings very easily, especially when the property names are the same.

CodePudding user response:

I can't comment, so I can't ask what error you're getting. However, from looking at it, it looks like a casting problem. You can cast anonymous types to other types. Using just new makes is anonymous instead specify they type that you are declaring new.

List<WebApi.Entities.AttachmentResponse> attachments = (
    from a in db.Attachments
    from t in db.Transactions.Where(t => a.EntityId == t.TransactionId && t.UserId == userId)
    where a.EntityId == EntityId
    select new WebApi.Entities.AttachmentResponse
    {
        a.AttachmentId,
        a.Filename
    }).ToList();
  • Related