十进制?可为null的对象必须有一个值



当会议对象具有所有FlatFees的值时,我的逻辑工作正常。但是,当我删除某些会议的FlatFee时,我会收到一个错误Nullable object must have a value.。我知道我需要检查FlatFee是否为空,但我如何对此进行选择?

//EF Data
List<MeetingEvent> Meeting = new List<MeetingEvent>();
Meeting.Add(new MeetingEvent() { MeetingId = 1, MeetingName = "A1", FlatFee=25000 });
Meeting.Add(new MeetingEvent() { MeetingId = 2, MeetingName = "A2", FlatFee = 45000 });
Meeting.Add(new MeetingEvent() { MeetingId = 3, MeetingName = "A3" });
Meeting.Add(new MeetingEvent() { MeetingId = 4, MeetingName = "A4" });
Meeting.Add(new MeetingEvent() { MeetingId = 5, MeetingName = "A5" });

//EF Data
List<Event> Event = new List<Event>();
Event.Add(new Event() { EventId=1, MeetingId = 1,  });
Event.Add(new Event() { EventId = 2, MeetingId = 2,  });
Event.Add(new Event() { EventId = 3, MeetingId = 3,  });
Event.Add(new Event() { EventId = 4, MeetingId = 4,  });
Event.Add(new Event() { EventId = 5, MeetingId = 5,  });

var listOfEvents = Event.Select(y => new MeetingEventViewModel
{
MeetingId = y.MeetingId,
FlatFee = Meeting.Where(f => f.MeetingId ==y.MeetingId).Select(s => s.FlatFee.Value).FirstOrDefault(),
}).ToList();

foreach (MeetingEventViewModel m in listOfEvents)
{
Console.WriteLine(m.MeetingId);
Console.WriteLine(m.FlatFee);
}
class MeetingEvent
{
public int MeetingId { get; set; }
public string MeetingName { get; set; }
public decimal? FlatFee { get; set; }
}
class Event
{
public int EventId { get; set; }
public int MeetingId { get; set; }
public string Name { get; set; }
public MeetingEvent MeetingEvent { get; set; }
}
class MeetingEventViewModel
{
public int MeetingId { get; set; }
public string MeetingName { get; set; }
public decimal? FlatFee { get; set; }
}

您可以过滤掉空值,因此:

FlatFee = Meeting
.Where(f => f.MeetingId ==y.MeetingId)
.Select(s => s.FlatFee.Value)
.FirstOrDefault(),

变为:

FlatFee = Meeting
.Where(f => f.MeetingId ==y.MeetingId && f.FlatFee.HasValue)
.Select(s => s.FlatFee.Value)
.FirstOrDefault(),

或者设置默认值('0'?(,其中FlatFee为null,因此:

FlatFee = Meeting
.Where(f => f.MeetingId ==y.MeetingId)
.Select(s => s.FlatFee.Value)
.FirstOrDefault(),

变成这样:

FlatFee = Meeting
.Where(f => f.MeetingId ==y.MeetingId)
.Select(s => s.FlatFee.HasValue ? s.FlatFee.Value : 0)
.FirstOrDefault(),

你能试着这样重新定义你的模型吗?默认情况下,FlatFee的值始终为空。

class MeetingEvent
{
public int MeetingId { get; set; }
public string MeetingName { get; set; }
public decimal? FlatFee { get; set; } = null;
}
class Event
{
public int EventId { get; set; }
public int MeetingId { get; set; }
public string Name { get; set; }
public MeetingEvent MeetingEvent { get; set; }
}
class MeetingEventViewModel
{
public int MeetingId { get; set; }
public string MeetingName { get; set; }
public decimal? FlatFee { get; set; } = null;
}

您可以检查是否有值的FlatFee。如果它没有设置值0而不是NULL

var listOfEvents = Event.Select(y => new MeetingEventViewModel
{
MeetingId = y.MeetingId,
FlatFee = Meeting.Where(f => f.MeetingId == y.MeetingId).Select(s => s.FlatFee.HasValue ? s.FlatFee.Value : 0 ).FirstOrDefault(),
}).ToList();

或者获取具有值而不是NULLFlatFee记录

foreach(MeetingEventViewModel m in listOfEvents.Where(m=> m.FlatFee.HasValue))
{
Console.WriteLine(m.MeetingId);
Console.WriteLine(m.FlatFee);
}

另一个选项是获取FlatFee有值的记录。在中添加f.FlatFee.HasValue其中

var listOfEvents = Event.Select(y => new MeetingEventViewModel
{
MeetingId = y.MeetingId,
FlatFee = Meeting.Where(f => f.MeetingId == y.MeetingId && f.FlatFee.HasValue).Select(s => s.FlatFee.Value ).FirstOrDefault(),
}).ToList();

您可以在Nullable类型中使用DBNull.Value进行控制。

foreach (MeetingEventViewModel m in listOfEvents)
{
Console.WriteLine(m.MeetingId);
Console.WriteLine(m.FlatFee == DBNull.Value ? 0 : m.FlatFee);
}

也许,这对你的工作有用。

编辑:如果要使用Console.Writeline。您应该使用此m.FlatFee.ToString((Console.WriteLine(m.FlatFe==DBNull。Value?0:m.Flat费.ToString(((;但是,如果你想要一个十进制变量,你应该转换这个值。像这样;if(m.FlatFee!=DBNull.Value({FlatFee=Convert.ToDecimal(m.FlatMee(;}

相关内容

最新更新