First of all I need to say sorry about my English.
I have a strange problem similar to this one (http://forums.asp.net/t/1313183.aspx), but I can't figure out the answer given in thread. So I decided to post my own here
I have a DataBase with table called "Datchik". I want to receive some information from fields called "id_tip_datchika" and "id_se"
Here is the problem:
When I compile with this code:
List<Datchik> sensorByQueryList = (from s in connect.Datchik
where s.id_tip_dat == 9
&& s.id_se == null
select s).ToList();
I get an information from my DataBase with this query.
But when I compile with this one, where I added an int? variable called "parameter"=null in "where" statement, I get clearly nothing in my DataGrid:
int? parameter = null;
// Инициализируем список датчиков
List<Datchik> sensorByQueryList = (from s in connect.Datchik
where s.id_tip_dat == 9
&& s.id_se == parameter
select s).ToList();
so when I put null manually everything works fine, but when I do similar query with using of variable "parameter" I get nothing.
Please help me figure out whats happening, I'm really stucked with it
Looking at the post you referenced, it seems like you can't pass a null variable into the query.
So your query would have to look like this:
int? parameter = null;
//now you would have to check if the parameter has value or if it is null
if(parameter.HasValue)
{
//it is not null so you can pass in the parameter
List<Datchik> sensorByQueryList = (from s in connect.Datchik
where s.id_tip_dat == 9
&& s.id_se == parameter
select s).ToList();
{
else
{
//it is null so you have to manually pass in 'null'
List<Datchik> sensorByQueryList = (from s in connect.Datchik
where s.id_tip_dat == 9
&& s.id_se == null
select s).ToList();
}
Unfortunately your advice can't solve my problem. Let me explain what I want to do in my program. It would be great if anyone can advise me.
I have at least ten combo-boxes and text-boxes with data entered by user. Independently of which of them are filled, LINQ-query to my database must fill datagrid with relevant information.
----------------------------------------------- etc. --------------------------------------------------------
For example, if ComboBox1 filled with "5" and ComboBox2 filled with "7" while other are empty (in other words they contain null values which I need very much) my only LINQ-query must return "Name2". In that case my query shoud look like that:
int? parameter1; // = 5
int? parameter2; // = 7
int? parameter3; // should be null
List<Datchik> sensorByQueryList = (from s in connect.Datchik
where s.ComboBox1Value == parameter1
&& s.ComboBox2Value == parameter2 && s.ComboBox3Value == parameter3
select s).ToList();
So idea is to make a one universal query to avoid many if-else or case expressions. So if there will be a case when all the values from table are not null (like in first row), my only query could work well again.
Congratulation that you've solved your problem. Welcome to our forum again and I'll close the issue. if you have anything urgent, please create another new thread to conginue.
whiet
0 Points
3 Posts
Null value from int? variable in LINQ query
Nov 29, 2012 08:31 PM|LINK
Hi!
First of all I need to say sorry about my English.
I have a strange problem similar to this one (http://forums.asp.net/t/1313183.aspx), but I can't figure out the answer given in thread. So I decided to post my own here
I have a DataBase with table called "Datchik". I want to receive some information from fields called "id_tip_datchika" and "id_se"
Here is the problem:
When I compile with this code:
List<Datchik> sensorByQueryList = (from s in connect.Datchik where s.id_tip_dat == 9 && s.id_se == null select s).ToList();I get an information from my DataBase with this query.
But when I compile with this one, where I added an int? variable called "parameter"=null in "where" statement, I get clearly nothing in my DataGrid:
int? parameter = null; // Инициализируем список датчиков List<Datchik> sensorByQueryList = (from s in connect.Datchik where s.id_tip_dat == 9 && s.id_se == parameter select s).ToList();so when I put null manually everything works fine, but when I do similar query with using of variable "parameter" I get nothing.
Please help me figure out whats happening, I'm really stucked with it
msmk
Participant
776 Points
158 Posts
Re: Null value from int? variable in LINQ query
Nov 29, 2012 08:42 PM|LINK
Looking at the post you referenced, it seems like you can't pass a null variable into the query.
So your query would have to look like this:
int? parameter = null; //now you would have to check if the parameter has value or if it is null if(parameter.HasValue) { //it is not null so you can pass in the parameter List<Datchik> sensorByQueryList = (from s in connect.Datchik where s.id_tip_dat == 9 && s.id_se == parameter select s).ToList(); { else { //it is null so you have to manually pass in 'null' List<Datchik> sensorByQueryList = (from s in connect.Datchik where s.id_tip_dat == 9 && s.id_se == null select s).ToList(); }whiet
0 Points
3 Posts
Re: Null value from int? variable in LINQ query
Nov 29, 2012 11:20 PM|LINK
Thanks for reply!
Unfortunately your advice can't solve my problem. Let me explain what I want to do in my program. It would be great if anyone can advise me.
I have at least ten combo-boxes and text-boxes with data entered by user. Independently of which of them are filled, LINQ-query to my database must fill datagrid with relevant information.
Here is an example of my database table:
____________________________________________________________________________________________________________
Values for ComboBox1 | Values for ComboBox2 | Values for ComboBox3 | ... | NAME |
-------------------------------------------------------------------------------------------------------------
3 | 1 | 2 | ... | Name1 |
-------------------------------------------------------------------------------------------------------------
5 | 7 | null | ... | Name2 |
----------------------------------------------- etc. --------------------------------------------------------
For example, if ComboBox1 filled with "5" and ComboBox2 filled with "7" while other are empty (in other words they contain null values which I need very much) my only LINQ-query must return "Name2". In that case my query shoud look like that:
List<Datchik> sensorByQueryList = (from s in connect.Datchik where s.ComboBox1Value == parameter1 && s.ComboBox2Value == parameter2 && s.ComboBox3Value == parameter3 select s).ToList();So idea is to make a one universal query to avoid many if-else or case expressions. So if there will be a case when all the values from table are not null (like in first row), my only query could work well again.
Many thanks in advance!
whiet
0 Points
3 Posts
Re: Null value from int? variable in LINQ query
Nov 30, 2012 02:33 PM|LINK
Thanks for help!
So, I've solved this problem!
The solution was to edit linq-query:
List<Datchik> sensorByQueryList = (from s in connect.Datchik where s.id_tip_dat == 9 && (!parameter.HasValue && s.id_se == null) ^ (parameter.HasValue && s.id_se == parameter) select s).ToList();everything works correctly now!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Null value from int? variable in LINQ query
Dec 01, 2012 12:00 AM|LINK
Hello,
Congratulation that you've solved your problem. Welcome to our forum again and I'll close the issue. if you have anything urgent, please create another new thread to conginue.
Reguards!