Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I Have LINQ Query as below
C#
if(Status==1)
{

var Lqry = from A in DC.CLRN_Tables
           join B in DC.CLRN_Tables2 on R.ID equals B.ID
           where (A.STATUS == "1") && (B.iStatus == 1)
}

if(Status==2)
{
var Lqry = from A in DC.CLRN_Tables
           join B in DC.CLRN_Tables2 on R.ID equals B.ID
           where (A.STATUS == "1") &amp(B.iStatus == 2)
}

But based on the status value query will change , i Need separate query for based on the status. But I want to fill the dataset using the same query.
C#
SqlCommand cmd = (SqlCommand)DC.GetCommand(Lqry); // This Lqry is common 
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dsTR);

But it shows error in Var Laqry

Please any one help
Posted
Updated 22-Aug-12 20:56pm
v2

That is because you are trying to declare Lqry twice in different scopes, and use it outside both of them.
Why not combine the two queries into one:
C#
var Lqry = from A in DC.CLRN_Tables
                          join B in DC.CLRN_Tables2 on R.ID equals B.ID
                          where (A.STATUS == "1") && (B.iStatus == Status)
 
Share this answer
 
Comments
V Mohan 23-Aug-12 4:25am    
No, In different scenario i join different tables but i select common fields from all. For that only i need a LINQ query for var Lqry as a general
Why are you using linq this way? If you need sql query, build it and pass to the command. Linq is not an alternate way to write sql statements. Any by the way you have select missing from both linq statements (is not necessary, but should be used), plus you declared the Lqry variable inside the block, thus out of scope of the second part of the code. And why have you made two queries for that?
 
Share this answer
 
v4
First, few things:
1. var is a compile time constant and its scope remains within if{...} or else{...} (in your case). In general within parenthesis {...}
2. The LINQ statement gets executed only when use the LHS variable in the statement.
4. Syntax of the statement is wrong.
3. Do not mix LINQ and SqlCommand. Use either of them.

So,
First,
C#
if(Status==1)
{
 
var Lqry = from A in DC.CLRN_Tables
           join B in DC.CLRN_Tables2 on R.ID equals B.ID
           where (A.STATUS == "1") && (B.iStatus == 1)
SqlCommand cmd = (SqlCommand)DC.GetCommand(Lqry); // This Lqry is common 
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dsTR);
}
 
if(Status==2)
{
var Lqry = from A in DC.CLRN_Tables
           join B in DC.CLRN_Tables2 on R.ID equals B.ID
           where (A.STATUS == "1") &(B.iStatus == 2)

SqlCommand cmd = (SqlCommand)DC.GetCommand(Lqry); // This Lqry is common 
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dsTR);
}

By doing this, the scope of Lqry remains within if

Second,
The LINQ statement gets executed when you use it. What I mean by this is, when you write this

C#
var Lqry = from A in DC.CLRN_Tables
           join B in DC.CLRN_Tables2 on R.ID equals B.ID
           where (A.STATUS == "1") && (B.iStatus == 1)

the statement does not execute.
To execute you need to use this and perform some actions on it. For example, Lqry.Count() perhaps.

Third,
Syntax: A valid statement would look like

C#
List<int> someCollection = new List<int>() { 3,6,9,12,1,17,4 };
var result = from x in someCollection
             select x;

result would be IEnumerable<int>
So, in the above query, the please change the syntax.
Perhaps,
C#
var Lqry = from A in DC.CLRN_Tables
           join B in DC.CLRN_Tables2 on R.ID equals B.ID
           where (A.STATUS == "1")&&(B.iStatus == 1)
           select A;


Fourth,
Do not mix LINQ and SqlCommand. It is always better to have either of them.

Fix these minor things and your code should work.
 
Share this answer
 
v3
Comments
V Mohan 23-Aug-12 5:06am    
Thank you so much
C#
var Lqry = from A in DC.CLRN_Tables
           join B in DC.CLRN_Tables2 on A.ID equals B.ID
           where (A.STATUS == "1")&&(B.iStatus == 1)
           select A;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900