方法system.datetime parse(system.string)没有支持到SQ的转换 [英] Method system.datetime parse(system.string) has no supported translation to SQ

查看:185
本文介绍了方法system.datetime parse(system.string)没有支持到SQ的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我想要运行此代码但错误此消息方法'System.DateTime Parse(System.String)'没有支持的SQL转换

I不得更改表格的明确但比较表格的字段在可靠的日期时间

Regrads

Yarian



代码C#:

Hello
I Want Runnig This Code but Error This Message "Method 'System.DateTime Parse(System.String)' has no supported translation to SQL"
I Must Not Changed Definiation Of Table But Compare Field Of Table By On Veriable Datetime
Regrads
Yarian

Code C# :

DataClasses1DataContext db = new DataClasses1DataContext();




DateTime D = new DateTime();
D = DateTime.Now;
var query = db.Tables.Where(x => DateTime.Parse(x.DT) > D).ToList();
foreach (var item in query)
{
    MessageBox.Show(item.DT);
}





表格架构:



Schem Of Table :

CREATE TABLE [dbo].[Table] (
    [DT] NVARCHAR (50) NULL
);





我的尝试:



IAm从varchar(50)更改字段到nvarchar(50)

但是我不应该更改为Datetime



What I have tried:

IAm Change Field From varchar(50) To nvarchar(50)
But Iam Not Should change To Datetime

推荐答案

在不了解您的问题/日期格式的情况下,我认为您有2个选项



1)将DateTime.Now变量转换为字符串形成做你的比较。虽然我认为这不会达到你想要的效果。



Ex:



var d = DateTime.Now.ToString(yyyyMMdd)



2)将.ToList放在查询的.Where子句之前。问题是.ToList是针对数据库执行的命令,并且DateTime.Parse没有Linq to实体转换。



如果你要这样做以下



Without knowing more about your problem/the format of your dates i think you have 2 options

1) Convert your DateTime.Now variable to the string formation to do your comparison. Although i don't think this will accomplish what you are wanting.

Ex:

var d = DateTime.Now.ToString("yyyyMMdd")

2) Place the .ToList before the .Where clause of your query. The issue is that the .ToList is the command to execute against the database and there is no Linq to entities conversion for DateTime.Parse.

If you were to do the following

var query = db.Tables.ToList().Where(x => DateTime.Parse(x.DT) > D);





然后它会在尝试将DateTime.Parse转换为SQL之前对数据库执行。但是,如果您处理大量数据,这可能会产生很多问题,因为这会在过滤之前将整个Tables表读入内存。



我不建议但是,假设您说您无法更改此架构。这将完成您的要求。



Then it would execute against the database prior to trying to translate DateTime.Parse to SQL. However this could create a lot of problems should you be dealing with a lot of data as this reads the entire Tables table into memory prior to filtering.

I would not suggest this but given that you say you cannot change this schema. This would accomplish what you are asking for.


我会说您应该更改数据库列...您可能会尝试找到更新所有应用程序的方法。



例如,您可以为以适当格式写入的日期添加新的可为空的列,然后更新所有更新以更新两列。更新所有应用程序后,您可以手动更新文本列中仍为空的任何项目。完成此操作后,您可以使该列成为必需(如有必要)。此时,您可以更新读取数据库的所有代码以读取新列。完成后,您可以删除旧的文本列。



然后从那时起您将不再需要处理不正确的列类型,它将简化任何读取或更新数据库的新代码。



如果应用程序部署在许多位置(例如,在客户端计算机上),那么您可以在更新时更新数据库你的应用程序的新版本打开一个仍然使用旧列的数据库。



如果真的不可能这样做,那么我希望至少文本列是格式化为年,月和日的顺序,否则进行任何比较都会很痛苦。



您可能还会考虑执行存储过程或手动编写SQL代码会解决这个问题。也许可以使用Linq和SubString,但我不确定。
I would say that you should change the database column anyway... You might try to find a way to update all your application.

For example, you might add a new nullable column for date written in proper format and then update all update to update both columns. Once all applications are updated, then you can manually update any item that is still null from the text column. Once this is done, you can make that column required (if necessary). At that point, you can update all code that read the database to read the new column. Once this is done, you can remove the old text column.

Then from that point you won't have to deal with the improper column type anymore and it will simplify any ew code that read or update the database.

If the application is deployed at many locations (for example, on client computer), then you could update the database when a new version of your application open a database that still use old column.

If it is really impossible to do that, then I hope that at least the text column is formatted in year, month and day order as otherwise it would be a pain to do any comparison.

You might also consider doing a stored procedure or manually write SQL code that would fix the problem. It might also be possible to do with Linq and SubString but I am not sure.


这篇关于方法system.datetime parse(system.string)没有支持到SQ的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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