Inner Join in tables 1 - n

Asked

Viewed 173 times

1

I have two tables: Vehicle and Images, where a vehicle can have n images. When performing an Inner Join I get the following result: Vehicles1-Addressimage Vehicles1-Addressimage Vehicles2-Addressimage Vehicles2-Addressimage Vehicles2-Addressimage I wonder if there is any way to get the results this way: V1-All Vehicle Images1 V2-All Vehicle Images 2

I am developing a project in Asp net mvc and would like to send this last result to my view. Is there anything I can do to get the data this way.

  • Are you using json? If you’re just creating an array to structure the way you want and send.

  • Could you post the part of the code where you retrieve the information from the bank and the part where it will display in the View? so it’s easier to understand what’s being done

  • public Actionresult Index() { Return View(db.Veiculo.Tolist()); }

  • 1

    Put this information in the questions... but come on.. in your View she’s typified as Icollection Vehicle, right? you can do something like this @foreach (var imagem in item.Imagens). in case would be inside foreach, ie for each vehicle line would pass a list of images of that vehicle

1 answer

1

As far as I’m concerned, it’s just an ordination problem?

If all vehicles have at least one image, a inner join and at the end of the query a order by by the id/name/etc of the vehicle.

If a vehicle may not have an image then the left join to capture the cases of vehicles without image. Here would also use a order by by the id/name/etc of the vehicle.

I hope I’ve helped.

  • What I would really like is for the vehicle to appear with its respective images in one line and not in multiple lines. ?

  • 1

    In that case https://stackoverflow.com/questions/16855764/ms-sql-one-to-many-relationship-need-to-return-single-row

Browser other questions tagged

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