0

I have this query and I want select records with JobID equal variable of JobID or if JobID is null, select all records. How can I do it ?

var SearchPost = (from p in db.Posters
                  where p.CityID == CityID && p.PosterOK == 1 && p.PosterName.Contains(strSearch) && p.PosterImageCount > btnImage && p.JobID == JobID
                  join a in db.Likes
                  on p.PosterID equals a.PosterID into LikeSearch
                  join b in db.Comments
                  on p.PosterID equals b.PosterID into CommentSearch
                  select new { 
                    p
                    //For get Count of like
                    , like = LikeSearch.Where(a => a.PosterID == p.PosterID).Count()
                    //For get AVG from Comments
                    , CommentCount = CommentSearch.Where(b => b.PosterID == p.PosterID).Count()
                    , price = (int?)CommentSearch.Select(h => h.Price).Average() ?? 0
                    , quality = (int?)CommentSearch.Select(h => h.Quality).Average() ?? 0
                    , variety = (int?)CommentSearch.Select(h => h.Variety).Average() ?? 0
                   , morality = (int?)CommentSearch.Select(h => h.Morality).Average() ?? 0
             }); 

1 Answer 1

0

How about having an if condition and checking null and then add that query?

var SearchPost =  (from p in db.Posters
                           where p.CityID == CityID && p.PosterOK == 1 && p.PosterName.Contains(strSearch) && p.PosterImageCount > btnImage 
                           join a in db.Likes
                           on p.PosterID equals a.PosterID into LikeSearch
                           join b in db.Comments
                           on p.PosterID equals b.PosterID into CommentSearch
                           select new { 
                              p
                              //For get Count of like
                              , like = LikeSearch.Where(a => a.PosterID == p.PosterID).Count()
                              //For get AVG from Comments
                              , CommentCount = CommentSearch.Where(b => b.PosterID == p.PosterID).Count()
                              , price = (int?)CommentSearch.Select(h => h.Price).Average() ?? 0
                              , quality = (int?)CommentSearch.Select(h => h.Quality).Average() ?? 0
                              , variety = (int?)CommentSearch.Select(h => h.Variety).Average() ?? 0
                              , morality = (int?)CommentSearch.Select(h => h.Morality).Average() ?? 0
                           }); 

if(JobID !=null)
{
    var SearchPost1 = SearchPost.Where(p => p.JobID == JobID)
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.