LINQ - LINQ Optional Parameter not passing NULL correctly?

Asked By Nick S
02-Aug-10 09:45 AM
Hi,

I'm using LINQ in my application, and I have an optional int data type in my WCF web service.

So:

public List<Test> GetRecords(int seq1, int? seq2)

{

TestDataContext db = new TestDataContext ();

var myList = (from test in db.test

where test.SEQ_GROUP == seq1 && test.SEQ_STAFF == seq2
select new Test

{

Item = System.Convert.ToString(test.SEQUENCE),

ID = test.ID,

Description = test.DESCRIPTION,

Name = test.Name,

Desc = test.Desc

}

);


Now, if I execute this, the SQL basically queries the database and says SEQ_STAFF = 1

Howvever, if I don't populate seq2 as the optional integer... the SQL code executes as SEQ_STAFF = null

This should be executing SEQ_STAFF IS NULL


Can anyone help here?

THanks,

Nick

 

  Indranil Chatterjee replied to Nick S
03-Jan-11 06:24 AM
That is a common limitation of LINQ to SQL.
Try this instead:

from test in db.test

where test.SEQ_GROUP == seq1 && ((seq2 == null && test.SEQ_STAFF == null) || test.SEQ_STAFF == seq2)
select new Test

//The point is, when the comparison is explicitly done against null, LINQ to SQL generates a proper IS NULL clause

Create New Account
help
Wpf, Wcf, Mvvm, Linq HI, How to insert the data into tables using wpf, wcf, mvvmpattern and linq Refer these- WPF MVVM Pattern: A Simple Tutorial for Absolute Beginners MVVM - Commanding http: / / social keys etc) - this can be automated (see here) create a DataContext class that represents your database - you will need this to get and submit your changes to the database after you've made changes to your object model, submit the changes to the database using the data context. there are plenty of examples on the web, such as this msdn.microsoft.com / en-us / library / bb425822.aspx#linqtosql_topic7 Regards. Create New Account keywords: Wpf, Wcf, Mvvm, Linq description: HI, How to insert the data into tables using wpf, wcf, mvvmpattern
upload image to database , silverlight hi, in silverlight how can i upload image or file to database. i m using wcf service + LINQ for database connectivity. so how can i send the image to wcf service to upload. check this one explaining how to accomplish this. . http: / / silverlight.net / forums t / 64867.aspx Hi, 1. WCF + LINQ: are you going to store the image / file to database of to a physcial location
Beginner with Questions re: WPF, WCF, LINQ .NET Framework Hi All, I am coming from a PHP, Java background. I am just I am going and then I will start to learn everything. We have a DB2 database. I am assuming I can write a WCF service to access this database and return the data from the service to a WPF front end app (the service back in another format? If it sends back only in XML then can I use LINQ to XML to display the data in a WPF front end? I guess I just on how all these things work together. Thanks C# Discussions WebService (1) VB.NET (1) LINQ (1) WPF (1) WCF (1) Database (1) Serv.TestIt (1) WebMethod (1) Yes. Note though that a SOA web
WCF Data Services / WCF Behaviors And Server Side Processing Server Side Processing of Data in WCF Data Services Using WCF Behaviors Introduction One of the concerns around the data services is their ability to control the outgoing data. Unfortunately, Entity Framework provider of WCF Data Services does not support processing data before forwarding to the clients. Many applications need to control the outgoing data of Data Services. That approach uses the customized version of LINQ to SQL to do so. In this paper, I will show an approach that can Entity Framework Provider. The new approach processes the outgoing data using the facilities provided by WCF Behaviors. The advantage of using WCF behaviors is the fact that they deploy in a plug & play manner and they don
Is Linq to SQL any good? .NET Framework I am a big fan of linq but in my opinion linq to sql is a complete waste of time. Why on earth take something that is difficult to write by creating a translation layer. I see people here struggling to write linq to sql queries. It also creates the problem of being locked into a particular product with regards to the database which is exactly the problem I thought a translation layer would be designed to solve 1) Entity Framework (1) Visual Studio (1) Silverlight (1) Oracle (1) If we ignore the LINQ to SQL vs LINQ to EF aspect. You should use LINQ to EF not LINQ to SQL. .NET wanted an ORM. Many people including me like