On my setup have I have the following query.
Class:
public class NewsList
{
public Guid NewsGuid { get; set; }
public string Heading { get; set; }
public string FileName { get; set; }
}
Controller:
List<NewsList> newsList = (from n in db.News
join ni in db.NewsImages
on n.NewsGuid equals ni.NewsGuid
where (ni.FileOrder == 10 || ni.FileName == null) && n.NewsDate.Year == newsDate.Year && n.NewsDate.Month == newsDate.Month && n.NewsDate.Day == newsDate.Day
orderby n.NewsDate descending
select new NewsList
{
NewsGuid = n.NewsGuid,
Heading = n.Heading,
FileName = ni.FileName
}).Take(10).ToList<NewsList>();
I have two issues with this.
1) I am joining two tables here, which are News and NewsImages and its a 1-N relation. One news can have nothing, one or more images. With this joining, I don't get news without image even though I specified ni.FileName can be null. It only returns News, which has at least a record in the NewsImages table. I just want a regular type of LEFT JOIN here. How do I achieve that?
2) the line with the WHERE statement it looks terrible that I need to check the year, month and day separately. NewsDate is a DateTime field on the database and newsDate is date only format (yyyy-MM-dd). I just want to select all the news from a specific date(querystring).
Without EF I can achieve this with standard T-SQL like this:
CAST(NewsDate AS date)='" + newsDate + "'"
How do I clean that WHERE line better?
n.NewsDate.Date == newsDate.Datesuit your need in the where clause?