请问个关于linq查询指定字段的问题

啊哈五个字 2017-10-16 02:09:56
var s = db.t_Box.Where(u => u.Id == box).Select(p => new { p.Id, p.AuthoTime }).First();
AuthoTime是DateTime类型,可为空,问题是数据库中AuthoTime字段为null时,查询报错,这个语句应该怎么改。
...全文
915 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianlang_2008 2018-06-07
  • 打赏
  • 举报
回复
1、定义实体接受字段,其中时间的类型定义成可为空; 2、直接在select时将时间格式化为字符串格式;
码仔prince 2017-12-05
  • 打赏
  • 举报
回复
把First改成FirstOrDefault就行了
坤灵小舍 2017-12-05
  • 打赏
  • 举报
回复
public class ResultModel { public int Id{ get; set; } public Nullable<DateTime> AuthoTime{ get; set; } } var s = db.t_Box.Where(u => u.Id == box).Select(p => new ResultModel{ Id=p.Id, AuthoTime=p.AuthoTime }).First();
q107770540 2017-10-17
  • 打赏
  • 举报
回复
var s = db.t_Box.Where(u => u.Id == box).Select(p => new { p.Id, p.AuthoTime }).First(); ============= var s = db.t_Box.Where(u => u.Id == box).Select(p => new { p.Id, AuthoTime = p.AuthoTime==null?DateTime.MinValue: p.AuthoTime }).FirstOrDefault();
大然然 2017-10-16
  • 打赏
  • 举报
回复
如果你的表里未查询到数据,那么你的first()也会有问题 var query = db.t_Box.Where(u => u.Id == box).Select(p => new { p.Id, p.AuthoTime }); if(query.count() > 0) { var t = query.first(); }
exception92 2017-10-16
  • 打赏
  • 举报
回复
引用 8 楼 a462575515 的回复:
[quote=引用 6 楼 duanzi_peng 的回复:] [quote=引用 5 楼 a462575515 的回复:] [quote=引用 3 楼 duanzi_peng 的回复:] 在db的设计器中 设置表t_Box的AuthoTime的“可为null” 为true。
改了,public Nullable<System.DateTime> AuthoTime { get; set; },原本就是这样,如果数据库里该字段有数据好用,为null的话就报那个错[/quote] 那个错? 你倒是把错误贴出来啊,让我们猜么[/quote] Exception thrown: 'System.FormatException' in mscorlib.dll System.FormatException: 该字符串未被识别为有效的 DateTime。 [/quote] 数据库中的可空为Null 而非null。
exception92 2017-10-16
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
[quote=引用 5 楼 a462575515 的回复:] [quote=引用 3 楼 duanzi_peng 的回复:] 在db的设计器中 设置表t_Box的AuthoTime的“可为null” 为true。
改了,public Nullable<System.DateTime> AuthoTime { get; set; },原本就是这样,如果数据库里该字段有数据好用,为null的话就报那个错[/quote] 那个错? 你倒是把错误贴出来啊,让我们猜么[/quote] 你那样只是定义了一个可为空的 AuthoTime 属性,select中的new 表示要查询的是linqdb中的自动生成的那个部分类 t_Box,映射到的是这个类。生成的映射属性应该类似:


		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Name", DbType="VarChar(50) NOT NULL")]
		public string Name
		{
			get
			{
				return this._Name;
			}
			set
			{
				if ((this._Name != value))
				{
					this.OnNameChanging(value);
					this.SendPropertyChanging();
					this._Name = value;
					this.SendPropertyChanged("Name");
					this.OnNameChanged();
				}
			}
		}
如果设置正确,类似 DbType 中就不会有 NOT NULL
正怒月神 2017-10-16
  • 打赏
  • 举报
回复
引用 7 楼 a462575515 的回复:
[quote=引用 4 楼 hanjun0612 的回复:] 数据库设置可为null 模型设置 datetime?
原本就这样设的,不好使[/quote] 那是你数据的问题啊,和类型有什么关系、该字符串未被识别为有效的 DateTime 你存储的时间字符串不能转成datetime
啊哈五个字 2017-10-16
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
[quote=引用 5 楼 a462575515 的回复:] [quote=引用 3 楼 duanzi_peng 的回复:] 在db的设计器中 设置表t_Box的AuthoTime的“可为null” 为true。
改了,public Nullable<System.DateTime> AuthoTime { get; set; },原本就是这样,如果数据库里该字段有数据好用,为null的话就报那个错[/quote] 那个错? 你倒是把错误贴出来啊,让我们猜么[/quote] Exception thrown: 'System.FormatException' in mscorlib.dll System.FormatException: 该字符串未被识别为有效的 DateTime。 在 System.DateTimeParse.ParseExactMultiple(String s, String[] formats, DateTimeFormatInfo dtfi, DateTimeStyles style) 在 System.DateTime.ParseExact(String s, String[] formats, IFormatProvider provider, DateTimeStyles style) 在 System.Data.SQLite.SQLiteConvert.ToDateTime(String dateText, SQLiteDateFormats format, DateTimeKind kind, String formatString) 在 System.Data.SQLite.SQLite3.GetDateTime(SQLiteStatement stmt, Int32 index) 在 System.Data.SQLite.SQLite3.GetValue(SQLiteStatement stmt, SQLiteConnectionFlags flags, Int32 index, SQLiteType typ) 在 System.Data.SQLite.SQLiteDataReader.GetValue(Int32 i) 在 System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetUntypedValueDefault(DbDataReader reader, Int32 ordinal) 在 System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) 在 lambda_method(Closure , Shaper ) 在 System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) 在 System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext() 在 System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() 在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source) 在 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence) 在 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot) 在 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression) 在 System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression) 在 System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source) 在 Wisdom.AntiSteal.WebApp.WebApiApplication._server_DataReceived(Object sender, AsyncEventArgs e) 位置 D:\Projects\Wisdom.AntiSteal\Wisdom.AntiSteal.WebApp\Global.asax.cs:行号 113
啊哈五个字 2017-10-16
  • 打赏
  • 举报
回复
引用 4 楼 hanjun0612 的回复:
数据库设置可为null 模型设置 datetime?
原本就这样设的,不好使
exception92 2017-10-16
  • 打赏
  • 举报
回复
引用 5 楼 a462575515 的回复:
[quote=引用 3 楼 duanzi_peng 的回复:] 在db的设计器中 设置表t_Box的AuthoTime的“可为null” 为true。
改了,public Nullable<System.DateTime> AuthoTime { get; set; },原本就是这样,如果数据库里该字段有数据好用,为null的话就报那个错[/quote] 那个错? 你倒是把错误贴出来啊,让我们猜么
啊哈五个字 2017-10-16
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
在db的设计器中 设置表t_Box的AuthoTime的“可为null” 为true。
改了,public Nullable<System.DateTime> AuthoTime { get; set; },原本就是这样,如果数据库里该字段有数据好用,为null的话就报那个错
正怒月神 2017-10-16
  • 打赏
  • 举报
回复
数据库设置可为null 模型设置 datetime?
exception92 2017-10-16
  • 打赏
  • 举报
回复
在db的设计器中 设置表t_Box的AuthoTime的“可为null” 为true。
啊哈五个字 2017-10-16
  • 打赏
  • 举报
回复
有没有什么方法能拿到null而不报错
啊哈五个字 2017-10-16
  • 打赏
  • 举报
回复
Exception thrown: 'System.FormatException' in mscorlib.dll 该字符串未被识别为有效的 DateTime。

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧