为什么我需要转换这些返回的结果?
// Insert new record
ArtworkingDataContext dc = new ArtworkingDataContext();
tblArtworkData NewRecord = new tblArtworkData()
{
dateCreated = DateTime.Now,
templateID = TemplatesID,
userID = UsersID
};
dc.tblArtworkDatas.InsertOnSubmit(NewRecord);
// Set new values
this.Loaded = true;
this.ID = NewRecord.ID;
this.DateCreated = (DateTime)NewRecord.dateCreated;
this.TemplateID = (int)NewRecord.templateID;
this.UserID = (int)NewRecord.userID;
}
当我设置新值时,ID不需要转换,但其他值需要转换,否则我会得到:
Cannot implicitly convert type 'int?' to 'int'.
我已经厌倦了转换所有返回的数据,我做错了什么?为什么 ID 不需要演员表?
// Insert new record
ArtworkingDataContext dc = new ArtworkingDataContext();
tblArtworkData NewRecord = new tblArtworkData()
{
dateCreated = DateTime.Now,
templateID = TemplatesID,
userID = UsersID
};
dc.tblArtworkDatas.InsertOnSubmit(NewRecord);
// Set new values
this.Loaded = true;
this.ID = NewRecord.ID;
this.DateCreated = (DateTime)NewRecord.dateCreated;
this.TemplateID = (int)NewRecord.templateID;
this.UserID = (int)NewRecord.userID;
}
When I set the new values, ID doesn't need casting, but the others do, otherwise I get:
Cannot implicitly convert type 'int?' to 'int'.
I'm getting bored already of casting all my returned data, what am I doing wrong? And why doesn't the ID need a cast?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我认为这与 Linq 无关。编译器告诉您
Nullable
和T
之间不存在隐式转换。显然,对于正常值,转换很容易。但它不知道如果dateCreated
为null
该怎么办I don't think this is related to Linq. The compiler is telling you that there is no implicit cast between a
Nullable<T>
andT
. Obviously, for normal values, the cast is easy. But it doesn't know what to do ifdateCreated
isnull
TemplateID 和 UserID 不是 int,而是 System.Nullable,这就是为什么您必须强制转换它们。要么使 this.TemplateId 和 This.UserID
Nullable
或使用 Nullable 对象的 .Value 属性。
TemplateID and UserID are not int they're
System.Nullable<int>
and that is why you have to cast them. Either make this.TemplateId and This.UserIDNullable<int>
or use the .Value property on Nullable objects.
除了这里的所有其他答案之外 - 这可能表明数据库设计 Linq 到 Sql 映射中存在更大的问题。
您的对象显然希望始终提供这些值;但数据库列必须可为空。如果表中的数据永远不会为空,那么您应该将它们设置为 NOT NULL,然后在设计器中将每个属性的“Nullable”属性设置为“false”
然后您将不再需要强制转换或
.HasValue
检查。同样,您也可以这样做:
其中
default_Value
是某种哨兵整数 - 例如-1
或0
Apart from all the other answers here - this could indicate a bigger issue in the DB design an Linq to Sql Mapping.
Your object clearly expects these values to be provided all the time; but the database columns must be nullable. If the data going in the table will never be null, then you should make them NOT NULL and then set the 'Nullable' property in the designer for each of the properties to 'false'
Then you'll no longer need either a cast nor a
.HasValue
check.Equally, instead of these you can do:
Where
default_Value
is some kind of sentinel integer - e.g.-1
or0
您不需要将
int?
转换为int
,而是只需在对象上使用.Value
(或者更确切地说,< code>GetValueOrDefault() 扩展方法(如果这对您来说更安全)。该错误只是抱怨您将可为空的类型传递给不可为空的属性。You don't need to cast
int?
toint
, but rather, just use.Value
on the object (or, rather, theGetValueOrDefault()
extension method if that is safer for you). The error is just complaining that you are passing a nullable type to a property that is not nullable.在 C# 中,
NewRecord.templateID
和NewRecord.userID
的类型似乎是Nullable
或int?
。一个包装类,使
Int32
(在本例中)为 null。ID 字段可能在数据库中设置为“not null”。查看您的数据库架构,templateID 和 userID 可能允许 null,这些值会被转换为
Nullable
类型。可空结构
It seems
NewRecord.templateID
andNewRecord.userID
are of typeNullable<int>
orint?
in C#.A wrapper class that enables an
Int32
, in this case, to be null.The ID field is probably set as 'not null' in the database. Look at your DB schema, templateID and userID possibly allow nulls, which get translated to
Nullable<T>
types.Nullable Structure
如果数据记录中的值是
int?
类型,并且您要填充的对象具有int
类型的属性,那么您做错的第一件事正在铸造。并不是因为一直写(int)
很无聊。 当NewRecord.userID
包含null
时,将抛出
InvalidOperationException
。在那里,现在无聊的事情以一种你不想要的方式变得令人兴奋。对此有两种可能的反应:
“但这永远不会发生。”在这种情况下,请修复创建
NewRecord
的任何内容,使其属性不可为空。您可能会发现,是的,实际上它可能会发生,因为数据库中的基础列可以为空。“好吧,由于数据库列可以为空,我想这可能会发生。”在这种情况下,您需要找出该列可为空的原因。如果用户 ID 包含 NULL,这意味着什么?当它出现时,您的应用程序应该做什么?您可能会发现需要将本地对象的属性设置为
int?
而不是int
,然后包含处理其值为 null 的情况的逻辑。很多人建议您使用空合并运算符或其他分配默认值的方法。有时这是一个好主意 - 如果您知道 null 值的含义。但是使用
??
分配哨兵值的想法并没有真正解决问题,它正在将已经完全可用的哨兵值 -null
- 更改为一个值仅当它不能自然地出现在值空间中时才有效。有时您必须这样做 - 例如,如果您正在与一段不支持空值的代码对话 - 但这是您应该非常不情愿地做的事情。If the values in the data record are of type
int?
, and the object that you're populating has properties of typeint
, then the first thing you're doing wrong is casting. And not because writing(int)
all the time is boring. This:is going to throw a
InvalidOperationException
whenNewRecord.userID
containsnull
. There, now what was boring has become exciting in a way you didn't want.There are two possible responses to this:
"But that will never happen." In that case, fix whatever is creating
NewRecord
so that its properties aren't nullable. You'll probably find that yes, actually it can happen, because the underlying column in the database is nullable."OK, since the database column is nullable, I guess it can happen." In this case, you need to find out why the column is nullable. If the user ID contains NULL, what does this mean, and what should your application be doing when it is? You will probably find that you need to make your local object's properties
int?
instead ofint
, and then include logic that handles the case where their value is null.A lot of people have suggested that you use the null coalescing operator or some other way of assigning a default value. Sometimes that's a good idea - if you know what the null value is supposed to mean. But the idea of using
??
to assign a sentinel value isn't really solving the problem, it's changing what is already a perfectly serviceable sentinel value -null
- into a value that only works if it can't naturally occur in the value space. Sometimes you have to do this - if, for instance, you're talking to some piece of code that doesn't support null values - but it's something you should do very reluctantly.您必须检查可空值是否确实有值。
原因是 int != int?
整数?只是一个缩写形式
you have to check if the nullable values do have a value.
the reason is that int != int?
int? is just a short form for
我猜这些字段在数据库中是可为空的。
由于数据可能为空,因此 C# 将它们映射到可空数据字段 - 例如
int?
(这是Nullable
的简写),而不是强制转换它们,您应该可能使用
.HasValue
检查 null,然后使用.Value
属性 - 或者您可以使用 null 合并运算符 -??
如果您愿意为了避免这种转换,那么我能想到的唯一方法是更改数据库,使这些字段不能为空(也可能为它们提供默认值)
您的 ID 字段在数据库中不可为 Null -所以它在 C# 中由 and
int
表示,而不是int?
希望这对
Stuart有帮助
I guess that these fields are Nullable in the database.
Since the data could be null then the C# maps these to Nullable data fields - e.g.
int?
(which is shorthand forNullable<int>
)Instead of casting them, you should probably check for null using
.HasValue
and then use the.Value
property - or you could use the null coalescence operator -??
If you want to avoid this conversion, then the only way I can think of is to change your database so that these fields can't be null (and also possibly to provide a default value for them)
Your ID field isn't Nullable in the database - so it's represented by and
int
and not aint?
in your C#Hope that helps
Stuart
您需要类似
this.DateCreated = NewRecord.dateCreated.GetValueOrDefault();
You need something like
this.DateCreated = NewRecord.dateCreated.GetValueOrDefault();