想要使用“if” "否则"在linq, [英] want to use "if" "else" condition in linq,

查看:74
本文介绍了想要使用“if” "否则"在linq,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sql中我使用如下:

In sql I use like this:

Classcud2 ob1 = new Classcud2("select * from tbl_usertotalrating where store_name=''" + TxtCompany.Text + "''");
        if (ob1.ds.Tables[0].Rows.Count > 0)
        {
           // what i want
        }
        else
        {
         // what i want
        }



但我的问题是我想在linq中使用ifelse条件,

在linq中:


but my problem is I want to use "if" "else" condition in linq,
In linq :

var v = from k in li.category1s where k.cat_name == Ddlcategory.Text select k;



但是这个条件是对还是错var v总是有一些数据,

然后我怎么用这个


but whether this condition is right or wrong "var v " always have some data,
then how i use like this

if(v==null)
{

}
else
{

}



我理解我的问题,

请帮助我


I thing you understand my problem,
please help me

推荐答案

使用像这个



Use like this

if (v.Count() > 0)
 {
   //what you want
 }
 else
 {
   //what you want 
 }


当没有返回结果时,你有一个''空''列表。这与创建的新列表相同。



为此,您可以尝试以下任何一项:

When no results are returned, you have an ''empty'' list. This is same as a new list created.

For it, you can try any of the following:
if (!returnedResult.Any()) 
{
  // No result
}

//OR
if(returnedResult.Count() > 0)
{ 
  // Some result
}

//OR 
// convert result into a list
var returnedResult = (from Q in myDataContext select Q.ID).ToList();
if (returnedResult.Count > 0)
{
  // Some data returned
} 


这是一个地方,所以你应该使用强类型列表。然后使用ToList(),您可以检查列表中是否包含多个项目。或者只为var中的每一个做一个,并且在每一个中,将bool设置为true,这意味着你有数据。
This is a where, so you should use a strongly typed list. Then use ToList() and you can check if your list has more than one item in it. Or just do a for each on var, and in side the for each, set a bool to true, that will mean you have data.


这篇关于想要使用“if” "否则"在linq,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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