简体   繁体   English

在单个语句中正确使用Select()和Where()

[英]Proper usage of Select() and Where() in a single statement

I am trying to write a query in the controller similar to what I have in my SQL stored procedure: 我试图在控制器中编写一个类似于我的SQL存储过程中的查询:

Select * FROM TableName Where aDate >= beginDate and aDate <= endDate

In my controller, I have declared 2 variables, which will be getting input values from my view file: 在我的控制器中,我声明了2个变量,它们将从我的视图文件中获取输入值:

DateTime? startDate = input.BeginDateInput;
DateTime? endDate = input.EndDateInput;

and variable q that takes values from db 和变量q从db获取值

var q = db.tblName.AsQueryable();

Currently, I have a Select-based table of all values, looking like the following: 目前,我有一个基于Select的所有值的表,如下所示:

var data = q.Select(
  p =>
   new
   {
       //...
       WarrantyExpDate = p.WarrantyExpDate.ToShortDateString(),
       //...
   });

When I was trying to add .Where() at the end of 当我试图添加.Where()时

Select(...).Where(DateTime.Compare(Convert.ToDateTime(WarrantyExpDate), (DateTime)startDate)>0)

(shorten for time sake), I was receiving an error WarrantyExpDate was not in the current context . (缩短时间的缘故),我收到错误WarrantyExpDate was not in the current context After performing some research, I tried to recreate the suggestion from that thread to swap select and where, but it was returning the same issue. 在进行了一些研究后,我尝试从该线程重新创建交换select和where的建议,但它返回了同样的问题。

Can anyone please point me in the right direction on what I am doing wrong? 任何人都可以指出我正确的方向我做错了什么? How do I made both Select and Where recognize the same variable? 如何使Select和Where识别相同的变量?

As BJ points out, if you're using lambda syntax, you need to provide a lambda expression to the Where method. 正如BJ所指出的,如果你使用lambda语法,你需要为Where方法提供一个lambda表达式。 Something like this will probably work: 像这样的东西可能会起作用:

var data = q
 .Where(p => p.WarrantyExpDate < startDate)
 .Select(
  p =>
   new
   {
       //...
       WarrantyExpDate = p.WarrantyExpDate.ToShortDateString(),
       //...
   });

Note that I'm putting the Where clause first. 请注意,我首先放置Where子句。 This allows you to perform the query on the original data, rather than converting it to a string and back. 这允许您对原始数据执行查询,而不是将其转换为字符串并返回。 This is often, but not always, more desirable. 这通常是,但并非总是如此,更令人满意。 Sometimes your Select does a lot of work to get a specific piece of information and you don't want to have to repeat that logic in the Where clause. 有时,您的Select会为获取特定信息做很多工作,并且您不希望在Where子句中重复该逻辑。 The nice thing about LINQ is that you get to choose what's easiest. 关于LINQ的好处是你可以选择最简单的东西。

You might also consider using Query syntax instead; 您也可以考虑使用Query语法; it ends up compiling down to pretty much the same thing, but it looks a little nicer when you've got big queries. 它最终编译成几乎相同的东西,但是当你有大问题时看起来好一点。

var data = 
  from p in q
  where p.WarrantyExpDate < startDate
  select new
  {
   //...
   WarrantyExpDate = p.WarrantyExpDate.ToShortDateString(),
   //...
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM