I just started to learn some Odata and I need help at some queries I made.
Let’s take 2-3 examples of Linq queries. (NOTE: List of queries )
1) var one = Users.Single(s => s.ID == 1).Sources.Where(s =>s.Genre.Name == "Sport").ToList();
2) var second = TvSchedules.Where(s => s.Date.Day == DateTime.Today && s.Source.Type == "Channel"). Select(s => new {s.Media.Title,s.Media.Details.Description,s.Media.Images.FirstOrDefault(a=>a.ID==s.Media.ID).Link, s.Source.Sign}).ToList();
3) var third = TvSchedules.Where(s => s.Date < week && s.Media.Metadata.Select(a => a.IsHD).Contains(true)).Select(s => new { s.Media.Title, s.Media.Details.Description, s.Media.Images.FirstOrDefault().Link, s.Media.Metadata.FirstOrDefault().PriceSD, s.Media.RTRating }).ToList();
What are those queries for ? ( NOTE: Explaining list of queries )
1) I am selecting all Sources from User with ID=1 where source genre = 'Sport' ( I use User and Sources database tables )
2) I am querying only TVSchedules from Today where TVSchedules.Sources.Name = 'Channel' and I am selecting from TvSChedules.Media ( Title ), TvSchedules.Media.Details( Description), TvSchedules.Media.Images( Link ), TvSchedules.Source.Sign
In TvSchedules database table I have 2 FK ( MediaID, SourceID )
3) Like the second one, but I add some filters. I want to check if TvSchedules.Media.Metadata.isHd ==true
My problems (NOTE: Problems based on queries above )
Problem A) Are those queries correct in Linq? I’m not sure about Many-to-Many relationships.
For example in the third example where I have a many-to-many relation beetween Media and Metadata ( TvSchedules.Where(s => s.Date < week && s.Media.Metadata.Select(a => a.IsHD).Contains(true)) ) . I need to select only TvSchedules that have date < week and Media.Metada.isHd=true .
My TvSchedules table looks like this
ID MediaID SourceID Date
-- ------ -------- ----
Problem B) How can I translate those queries in ODATA ( Open data protocol ) queries ?
1) /Users(1)/AvailableSources?&filter=Genres eq 'Sport'
2) /TvSchedules?&filter=Date eq '@today'?$expand=Media,Details,Images?$select=Title,Description,Link
3) ?
Thank you
That kind of queries, except the first one, are syntactically and semantically correct but they are also problematic for readability, performance and because it is hard to compose the odata filter criteria.
I walked that road and more you go forward, more pain to feel. My recommendation is to create sql views for they:
After this, map your entities and you will be able to make it easier:
OData queries will be easier too. Please consider my recommendation.
Good luck!