Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Specific object after a join using LINQ

I am trying to retrieve a specific class related to a table in Entity Framework using linq for join and where conditions as follows:

                var result = (from a in db.Persons
                             join b in db.Person_IDs on a.PersonId equals b.PersonId
                             where b.FaceId == faceId
                             select new
                             {
                                 PersonId = a.PersonId,
                                 Name = a.Name,
                                 Address = a.Address,
                                 Picture = a.Picture,
                                 City = a.City,
                                 Estate = a.Estate,
                                 Phone = a.Phone,
                                 CellPhone = a.CellPhone,
                                 BlackList = a.BlackList
                             }
                             ).FirstOrDefault();

I would like the "result" object being returned as a Person object. In the above case, I need to create a new Person object and add the fields that came from the result.

Is it possible? I tried some ways and using some samples and researches but none of all alternatives worked for me.

Thanks!

UPDATE 1

All right, after some readings, the best way I found to do this was creating a DTO class for my Person object and returning this DTO class in my funcion as follows:

PersonDTO result = (from a in db.Persons
                             join b in db.Person_IDs on a.PersonId equals b.PersonId
                             where b.FaceId == faceId
                             select new PersonDTO
                             {
                                 PersonId = a.PersonId,
                                 Name = a.Name,
                                 Address = a.Address,
                                 Picture = a.Picture,
                                 City = a.City,
                                 Estate = a.Estate,
                                 Phone = a.Phone,
                                 CellPhone = a.CellPhone,
                                 BlackList = a.BlackList
                             }
                             ).FirstOrDefault();

            db.Dispose();

            return result;

All right, it worked fine, but one thing bothers me: why create another class identical to the EF class? Why can't EF class be used this way?

I am working with one table, but a program with, for example, 20 tables will force me to have 20 entity classes and 20 entity DTO classes!

As a beginner, I think this way of work a bit disorganized or nonsensical, making the traditional way (using data readers, commands and connections). Even being more bureaucratic, it don't have de need of "duplicated" objects.

Can somebody please provide this answer?

UPDATE 2

As requested: as I don't wat an anonymous type returning in my function, I tried returning the entity class (Person), but when I do this I get the following error in my application execution:

"The entity or complex type 'Models.Person' cannot be constructed in a LINQ to Entities query."

So the solution for this was create a DTO class (or a viewmodel class, whatever).

like image 437
Lucas Neves Avatar asked Oct 27 '25 07:10

Lucas Neves


1 Answers

If you have Person objects in your database, and you want Person objects, why are you going to the trouble of creating an anonymous type?

Why not just try

var result = (from a in db.Persons
              join b in db.Person_IDs on a.PersonId equals b.PersonId
              where b.FaceId == faceId
              select a).FirstOrDefault();
like image 133
oerkelens Avatar answered Oct 29 '25 22:10

oerkelens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!