如何在linq中使用where子句? [英] How to use where clause in linq?

查看:75
本文介绍了如何在linq中使用where子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试传递以下查询字符串api / test?cvr = cvr& cvr = trd,但我一直在获取一个空的数据数组。



如果我搜索api / test?cvr = cvr,我得到10个结果,如果搜索api / test?cvr = trd,我得到1个结果,当我一起搜索它时,我得不到数据。



  public  IEnumerable< database_data> GetType( string  cvr)
{
DateTime yesterday = DateTime.Today.AddBusinessDays(-1);

var data = 来自 c in db.database_data
其中 c.UploadDate ==昨天
其中(c.Cover == cvr ||
c.Cover == cvr)
select c;
return data.ToList();
}





我似乎无法使用此功能。任何帮助将非常感谢。

谢谢

解决方案

看看此示例 [ ^ ],取自MSDN页面 101 LINQ样本 [ ^ ]



编辑:您确定数据库中有昨天的数据条目吗?


根据问题编辑进行编辑。



您可以尝试通过不同的方式调试linq查询以检查它是否返回行。



一种简单的方法是通过将查询拆分为调试检查



 var data = 来自 c 中 db.database_data 
其中 c.UploadDate ==昨天
选择 c ;
data = data。 where (c.Cover == cvr || c.Cover == cvr)// for 调试目的将结合到一次检查





检查条件是否满足。在检查之前,在应用过滤器之前返回记录。


好吧,要在where语句中组合子句,您需要做的就是:

  var  data = 来自 c   db.database_data 
其中 c.UploadDate ==昨天&&
c.Cover == cvr
select c;

但是,您可能会发现测试时遇到问题反对UploadDate。您昨天存储的值包括时间部分,因此如果UploadDate实际上是日期,请在测试中使用

 c.UploadDate.Date == yesterday.Date 


I am trying to pass the following query string "api/test?cvr=cvr&cvr=trd", but i keep getting an empty array of data.

if I search api/test?cvr=cvr, i get 10 results and if search api/test?cvr=trd, i get 1 result and when I search them together i get no data.

public IEnumerable<database_data> GetType(string cvr)
       {
           DateTime yesterday = DateTime.Today.AddBusinessDays(-1);

           var data = from c in db.database_data
                      where c.UploadDate == yesterday
                      where (c.Cover == cvr ||
                       c.Cover == cvr)
                      select c;
           return data.ToList();
       }



I cannot seem to get this functionality to work. Any help would be very much appreciated.
Thank you

解决方案

Have a look at this sample[^], taken from the MSDN page 101 LINQ Samples[^]

Edit: Are you sure that there are data entries from yesterday in your DB?


Editing according to the Question Edit.

You Can try debugging the linq query by different ways to check if it returns rows.

One easy way is to debug by splitting the query to check

var data = from c in db.database_data
                       where c.UploadDate == yesterday
                       select c;
data =data.where (c.Cover == cvr || c.Cover == cvr)// for debugging purpose combine it to one once checking



Check whether the conditions are satisfied. Before that check that records are returned before applying filters.


Well, to combine clauses in a where statement, all you need to do is:

var data = from c in db.database_data
                       where c.UploadDate == yesterday &&
                        c.Cover == cvr
                       select c;

However, you might find that you get an issue with testing against UploadDate. The value you are storing in yesterday includes the time portion, so if UploadDate is actually a date, use

c.UploadDate.Date == yesterday.Date

in your test.


这篇关于如何在linq中使用where子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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