none
NULL values in parameters RRS feed

  • Question

  • Hi

    I have this service operation which returns me some values depending on two parameters.

        public myDataContract GetPr(string Name, int? fk)
            {
                myDataContext db = new myDataContext();

                return db.Pr
                        .Where(pr => pr.Name == Name && pr.Cust == fk)
                        .Select(pr => new MyDataContract {...}).SingleOrDefault();
            }


    When my parameter fk equals null, my generated SQL is
    Select ... from ... where Name = @p0 and Cust=@p1
    While it should be ... Cust is null.  Otherwise no records are selected. 

    Has someone a solution to make Linq understand this Nullable Int ?  Or an explanation why it won't work?

    Kind regards

    Monday, July 28, 2008 10:59 PM

Answers

  • Try:

     

    Code Snippet
    public myDataContract GetPr(string Name, int? fk)
            {
                myDataContext db = new myDataContext();

                return db.Pr
                        .Where(pr => pr.Name == Name && Object.Equals(pr.Cust, fk))
                        .Select(pr => new MyDataContract {...}).SingleOrDefault();
            }

     

     

    --Samir

    Tuesday, July 29, 2008 4:42 PM
  • Hi 

    Thanks for the answer.  I solved my problem this moring otherwise.

    The answer to the question is, "It is not supported".
    The reason (and two solutions) can be found in this post. ("query can not be compiled in advance")

    I haven't tried the solution with the object.Equal workaround (yes null == null).  So i don't know if it works.

    I choose the other solution:

    ==>  Split the .Where from the rest of the clause

    var prs = db.Pr.select( ...).Where(pr => pr.Name )
    if (fk.HasValue)
      prs = prs.Where(pr => pr.Cust == fk);
    return prs.SingleOrDefault();




    Tuesday, July 29, 2008 10:44 PM

All replies

  • Try:

     

    Code Snippet
    public myDataContract GetPr(string Name, int? fk)
            {
                myDataContext db = new myDataContext();

                return db.Pr
                        .Where(pr => pr.Name == Name && Object.Equals(pr.Cust, fk))
                        .Select(pr => new MyDataContract {...}).SingleOrDefault();
            }

     

     

    --Samir

    Tuesday, July 29, 2008 4:42 PM
  • Hi 

    Thanks for the answer.  I solved my problem this moring otherwise.

    The answer to the question is, "It is not supported".
    The reason (and two solutions) can be found in this post. ("query can not be compiled in advance")

    I haven't tried the solution with the object.Equal workaround (yes null == null).  So i don't know if it works.

    I choose the other solution:

    ==>  Split the .Where from the rest of the clause

    var prs = db.Pr.select( ...).Where(pr => pr.Name )
    if (fk.HasValue)
      prs = prs.Where(pr => pr.Cust == fk);
    return prs.SingleOrDefault();




    Tuesday, July 29, 2008 10:44 PM