0

我的 Linq 到实体查询的编写如下。我的 ORACLE 数据库中 DATECOLUMN1 的数据类型是字符串。

Datetime FilterStartDate = DateTime.Now;
            var query = from c in db.TABLE1
                        join l in db.TABLE2 on c.FK equals l.PK
                        where (FilterStartDate >= DateTime.ParseExact(l.DATECOLUMN1, "dd/MM/yyyy", CultureInfo.InvariantCulture) : false) == true
                        select c;

写上面的查询给我一个不支持的错误。如何将 DATECOLUMN1 转换为日期时间以进行比较。

PS 我无法控制数据库模式,因此更改 Oracle 数据库中列的数据类型对我来说不是一个可行的解决方案。

4

2 回答 2

1

在您的模型中,将以下属性添加到您的部分类TABLE2

public DateTime DATECOLUMN1_NEW
{
    get
    {           
        return  DateTime.ParseExact(DATECOLUMN1, "dd/MM/yyyy", CultureInfo.InvariantCulture);
    }

    set { }
}

然后,在您的 LINQ 查询中,使用DATECOLUMN1_NEW(它已经是 DateTime 格式)代替DATECOLUMN1.

于 2013-06-25T10:27:45.970 回答
0

Erm.. I think the problem you are having is that you are putting ": false" in there.

It looks like you are trying to use a condtional operator (?:) but you forgot the "?".

I don't think you actually need this as you are just trying to determine if the date is greater or not. Also if ParseExact fails it will throw an exception (not what you want) so you should use TryParse instead and handle the true/false returned and the out value to determine whether or not the date is (a) Actually a date (b) less then FilterStartDate.

You can use two alternatives:

  1. Use the function described in the answer here: How to I use TryParse in a linq query of xml data?

  2. Use the following fluent syntax version which I think is more readable.

    var query = db.Table1.Join(db.Table2, x => x.FK, y => y.PK, (x, y) => x).Where(x => { DateTime Result; DateTime.TryParse(x.Date, out Result); return DateTime.TryParse(x.Date, out Result) && FilterStartDate >= Result; });

于 2013-06-25T11:44:32.157 回答