I am trying to develop an API which will be taking few movie names as part of ViewModel and will be setting respective movie IDs in DTO.
The table representation for TBL_Movies (For example I have used only 5 data but in real there are quite few):
ID MovieName
1 Indiana Jones
2 John Wick
3 James Bond
4 Wolverine
The table representation for TBL_Transact:
Tab_ID MovieID1(int) MovieID2(int) MovieID3(int) MovieID4(int)
1 NULL NULL NULL NULL
The ViewModel structure:
{
Tab_ID: int,
MovieName1: "John Wick",
MovieName2: "Wolverine",
MovieName3: "Cast Away",
MovieName4: "A-Team"
}
The above ViewModel will be passed to the API PUT endpoint and now I need to fetch the respective MovieIDs for respective ViewModel fields and store them in DTO.
The DTO (MovieDetailUpdateDto) structure:
{
Tab_ID: int,
MovieName1: string,
MovieName2: string,
MovieName3: string,
MovieName4: string,
MovieID1: int,
MovieID2: int,
MovieID3: int,
MovieID4: int
}
So, DTO will be storing Movie ID in MovieID1 as the ID for the movie mentioned in the field MovieName1 and so on.
For example:
DTO.MovieID1 will be storing 2 (as "John Wick" is in MovieName1).
What I have tried:
I don't want to call the database for 5 or more times to get the IDs. So, I thought of using SQL IN operator and since am using EF-LINQ, so did something like this:
var movieDetailUpdateDto = _mapper.Map<MovieDetailUpdateDto>(ViewModel);
var customMovieValuesList = typeof(MovieDetailUpdateDto).GetProperties().Where(x => x.Name.ToLower().Contains("moviename")).ToList().Select(x => x.GetValue(movieDetailUpdateDto));
The above statement is giving me a list of the movie names but am unable to understand for which field the specific name is.
For example:
If the value "John Wick", in the list, is for the field MovieName1 or MovieName2 or any other MovieName fields.
So, I think using Dictionary I can be able to store the fieldname and its values. But, not sure how it will work or whether it will work or not?
Secondly, will it be possible to use "customMovieValuesList" in SQL IN operator using LINQ?
Thirdly, how to assign those MovieIDs back to DTO into respective fields using LINQ?