Masriyah
Masriyah

Reputation: 2525

using linq to sql to specify where clause

I am using this method to get results to fill my grid but this method is also used to fill another grid which requires a where clause with two params and this one only needs one. Even though i passed in null of the param that isn't used but still it is returning no results because of the where clause. Any advice of how i could change this maybe use linq to sql where i call the method to specify the where clause instead of in the method getting data?

DocsForReview.DataSource = docLib.GetGrid(Guid.Empty, lib); 

using (var dc = new DocMgmtDataContext())
{
    var subs = (from doc in dc.Documents
                join u in dc.Users on doc.OwnedByUserID equals u.ID
                where doc.OwnedByUserID == usr &&  doc.LibraryID == lib
                select new StudentDocuments
                {
                    DocID = doc.ID,
                    Assignment = doc.Library.Name,
                    Submitted = doc.UploadDT,
                     Student = u.FullName
                 }).OrderByDescending(c => c.Submitted).AsEnumerable().ToList();
    return subs;
}

Upvotes: 1

Views: 118

Answers (1)

Andrea Colleoni
Andrea Colleoni

Reputation: 6021

For nullable types try this:

doc.LibraryID == (lib ?? doc.LibraryID)

In your case (a System.Guid) you can try this:

doc.LibraryID == (lib == Guid.Empty ? doc.LibraryID : lib)

Upvotes: 2

Related Questions