C# 两个双精度值之间的 LINQ to SQL 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1414893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:17:31  来源:igfitidea点击:

LINQ to SQL value BETWEEN two double values

c#linqlinq-to-sql

提问by Alex

I'm using LINQ to SQL to query my database, I have a query very similar to this:

我正在使用 LINQ to SQL 查询我的数据库,我有一个与此非常相似的查询:

var result = from db.MyTable.Where(d => (double)d.Price >= minValue)

I need the where clause to have a d.Proce >= minValue, and d.Price =< maxValue(like a T-SQL BETWEENclause).

我需要 where 子句有一个d.Proce >= minValue, and d.Price =< maxValue(就像一个 T-SQLBETWEEN子句)。

How can I do this?

我怎样才能做到这一点?

采纳答案by cjk

How about this:

这个怎么样:

var result = from db.MyTable.Where(d => (double)d.Price >= minValue 
                                         && (double)d.Price <= maxValue)

回答by Marc Gravell

Just for completeness; if you are building a query based on different inputs you can compose it with successive Wherecalls:

只是为了完整性;如果您要基于不同的输入构建查询,则可以通过连续Where调用来组合它:

IQueryable<SomeType> query = db.MyTable;
if(minValue != null) // a Nullable<double>
{
    var actualMin = minValue.Value;
    query = query.Where(d => (double) d.Price >= actualMin);
}
if(maxValue != null) // a Nullable<double>
{
    var actualMax = maxValue.Value;
    query = query.Where(d => (double) d.Price <= actualMax);
}
// keep working with "query", for example, query.ToList();