LINQ一般查询错误 [英] LINQ General query Error

查看:97
本文介绍了LINQ一般查询错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有如下LINQ查询

Hi

I Have LINQ Query as below

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.

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


但是它显示了Var Laqry中的错误

请任何人帮忙


But it shows error in Var Laqry

Please any one help

推荐答案

这是因为您试图在不同的范围内两次声明Lqry,并在两个范围之外使用它.
为什么不将两个查询合并为一个:
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:
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)


您为什么要使用linq?如果您需要sql查询,请构建它并传递给命令. Linq不是编写sql语句的替代方法.两种方式都可能使您从两个linq语句中丢失 select (不是必需的,但应该使用),再加上您在块内声明了Lqry变量,因此超出了第二部分的范围代码.以及为什么要对此进行两次查询?
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?


首先,几件事:
1. var是一个编译时间常数,其范围保持在if {...}或else {...}(在您的情况下)之内.一般在括号内{...}
2.仅当在语句中使用LHS变量时,LINQ语句才会执行. 4.语句的语法错误.
3.请勿混合使用LINQ和SqlCommand.使用它们之一.

所以,
首先,
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,
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);
}


这样,Lqry的范围将保留在if

第二,
使用LINQ语句时将执行它.我的意思是,当你写这个


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

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)


该语句不执行.
要执行,您需要使用它并对其执行一些操作.例如,可能是Lqry.Count().

第三,
语法:有效的语句如下所示:


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

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


result将是IEnumerable<int>
因此,在上面的查询中,请更改语法.
也许,


result would be IEnumerable<int>
So, in the above query, the please change the syntax.
Perhaps,

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;



四,
不要混合使用LINQ和SqlCommand.最好都拥有它们.

修复这些小问题,您的代码应该可以使用.



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.


这篇关于LINQ一般查询错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆