I have been wrangling with this for a while, but I just can’t find a solid example to work from so I’m turning hopefully to the experts. Appreciation in advance for any help.
I have a working SQL query that I need to convert to Linq. Initially, I had a simple linq query driving an MSChart (line chart) that showed the daily ranking of an item in a list. Unfortunately, MSChart doesn’t drop days with null values off the chart, but just connects them to the next day with a non-null value, so I have to replace the nulls with 0s.
The following query does this via a Left, Self Join against the table, where I just grab the distinct list of days (which will include all days). I know I could set up an ancillary table with a list of all days as well, but this should be just as effective in all cases. The SQL that follows works perfectly in SSMS for a specific book/List combination:
select L2.date, ISNULL(L1.ranking,0)
from (select distinct date from ListItem where ListID = 1) L2
Left join ListItem L1 on L2.date = L1.date and L1.BookID = 1 and L1.ListID = 1
order by date
This returns a list of all dates and rankings, with a 0 ranking for any date that no ranking exists. Now in order to bind the chart to this, I believe converting to Linq is the best course.
I would post my attempts so far, but I fear they are woefully wrong. If any more information is necessary, please let me know.
Thanks.
-Dan
Something like this?
(Made a few changes since 1st rev.)