使用下面的代码,我将从MS 2005返回一个nvarchar字段,并继续获得一个System.InvalidCastException。
vo.PlacementID = dr.IsDBNull(0) ? null : dr.GetString(0);
vo.PlacementID变量是String类型的,所以应该没有问题。我试图返回的值如下(数字、数字、字母):00F、02A、01F等。
System.InvalidCastException: Unable to cast object of type 'System.Int32' to
type 'System.
我得到了这个错误
Unable to cast object of type 'System.Int32' to type 'System.String'.
执行例行LINQ查询时:
Return (
From n In DbContext.Newsletters
Where n.NewsletterID = NewsletterID
).FirstOrDefault
这是堆栈跟踪FWIW。
[InvalidCastException: Unable to cast object of type 'Sys
错误是
System.InvalidCastException occurred HResult=0x80004002 Message=Unable to
cast object of type 'System.String' to type 'System.Int32'. Source=<Cannot
evaluate the exception source> StackTrace: at
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.SimpleNullableDe
使用MVC3和StringLength验证属性(其内部值为string )类型为MyType<string>的属性,将得到一个异常:
无法将'MyType‘类型的对象转换为'System.String’。
所以我增加了一个操作符重载,这是我的课:
public class MyType<T>
{
public T Value { get; set; }
//...
public static implicit operator string(MyType<T> instance)
{
//R
因此,我试图让这个简单的测试在一个F#控制台应用程序中运行:
open System.Reflection
open System.ComponentModel.Composition
open System.ComponentModel.Composition.Hosting
[<Export(typeof<int -> string>)>]
let toString(i: int) = i.ToString()
[<EntryPoint>]
let main argv =
use catalog = new AssemblyCatal
这样做是可行的:
var i = (from x in db.Test
where x.Id == 1
select x).First();
db.Test.DeleteOnSubmit(i);
db.SubmitChanges();
我得到了这个(int/string)的强制转换错误:
var i = db.Test.Single(x => x.Id == 1);
db.Test.DeleteOnSubmit(i);
db.SubmitChanges();
我还能够成功地在同一张表上使用单个成功的方法进行更新。有什么线索吗?
更新
下面是更多的错误消息:
以下是更多内
我已经连接到SQL数据库表,并试图在模型的文件夹中为字符串CountryName创建数据注释。当它给我一个错误时:
System.InvalidCastException:无法将'System.Int32‘类型的对象转换为'System.String’。
namespace WorldEventsWeb.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial cla
我喜欢将类数组转换为整数数组。我的Data类是这样的- public class Data
{
public int low, high;
...................
...................
public static implicit operator int[](Data myClass)
{
int[] arr = {myClass.low, myClass.high};
return arr;
}
} 在这个class中,我有一个implicit operator con
我是.net新手,当我运行该程序时,它报告错误“无法将类型为System.Int32的对象强制转换为类型System.String”
导入System.Data导入System.Data.SqlClient导入System.Data.Sql分部类_Default继承System.Web.UI.Page
Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
Dim constr As String
constr = ConfigurationMa
我在数据库中有一张“卡片”表,其中有“卡片”和“客户”。卡包含CustomerID,号码和安放。在此之后,我使用ADO.NET来使用DataGrid、TextBox等。
我放弃了一个DataGrid和一个TextBox作为搜索条件。当按下按钮时,DataGrid将加载LINQ:
string searchString = this.CustomerIDTextBox.Text();
using (var ctx = new bdCustomerEntities()){
var result = from t in ctx.Cards
join t_cu
我想创建一个在运行时声明其类型的泛型List<>。
我可以执行以下操作,但由于它是动态的,我怀疑会有速度损失。我正在为一个奇异的数据库编写一个包装器,所以速度非常重要。
List<dynamic> gdb = new List<dynamic>()
我在动态泛型类型中读取了,但无法使其工作。具体地说,该对象不会以列表的形式出现,因此没有add方法。
Type ac;
switch (trail[dataPos].Type)
{
case GlobalsSubscriptTypes.Int32:
我有一个windows窗体应用程序,在该应用程序中,用户选择一个.csv文件并单击导入。此时,应该使用SqlBulkCopy将文件导入Server。问题是我遇到了一些错误,我认为这是因为导入时字段是空的。我得到了这个错误:
Input string was not in a correct format.Couldn't store <> in ScranDrugQuantity Column. Expected type is Int32.
这是CSV的一个样本。
974524,416194,131179,For information only. ,17/05/201
我有一个名为SystemUser的类,定义如下:
public class SystemUser
{
public int userID;
public string userName;
public string email;
public SystemUser(int userId, string userName, string email)
{
this.userID = userId;
this.userName = userName;
在用C#编写并为iPhone构建的C#项目中,我发现了一种奇怪的行为。
有时IList对象(实现IList接口的类型的对象)会丢失有关元素类型的信息。在我的研究中,我发现它发生在IList的打字机有一个位置(f.e。调用了从ArrayList到IList)和ToString()方法。
下面是我从脚本的Start()方法中调用的一个简单的测试方法。
public void IListToStringTest() {
ConsolePrintln("---\nIListToStringTest");
IList list = new ArrayList(); // <
qRow上出现错误。我如何用linq解决这个问题?我试图找到这个问题的答案,但没有运气。
IEnumerable<DataColumn> iCols = (from i in Interlocks
join r in Results on i.InterlockID equals r.InterlockID
where r.StationType.Equals("RT") && r.LineID.Equals("1") && r.TestNo == 1 && r.Stat
我正在制作一个游戏,其中包括允许玩家选择不同的选项和操作,然后执行它们以得到正确或错误(然后根据结果做其他事情)。
我(作为程序员)最终得到的是两个对象和一个以某种方式比较它们的请求。
public object arg0;
public CompareType compareType;
public object arg1;
args要么是int、float、string,要么是bool。假设CompareType是GREATER_THAN、EQUALS、LESS_OR_EQUAL_THAN等选项。默认情况下,无法比较的类型将转换为string,然后进行比较(可能会出现错误)。
我还没有想出
我有一个最奇怪的问题与EF核心3.1。在EFCore2.2中,我曾经能够执行存储过程。我看到文档中发生了急剧的变化,但是,我正在准确地跟踪文档,而且它不起作用。我在返回的数据中任何地方都没有空。NoticeOfInspection对象与返回的数据完全匹配。他们到底改变了什么,这是不起作用的?
var data = _dbContext.NoticeOfInspections.FromSqlRaw("EXEC dbo.NewReportApp_NoticeOfInspection {0}", FacilityId).Single();
错误消息根本没有帮助。首先使用上面的行,上面
我想使用N<T>而不是T?,但不能在带有<T>的函数中使用(T)显式强制转换
为什么(T)显式强制转换不能工作?如何修复它。
为什么是(1 is int?) == true。我怎么能让我的结构N有同样的力量。制作(1 is N <int> ) == true
using System;
public struct N<T> where T : struct
{
public T value;
public bool HasValue;
public static implicit operator N<T>(T
我对csharp和linq非常陌生,所以我遇到了一些简单的铸造问题。
我有个简单的目标-
public class AskQuestionFormView
{
public string QuestionText { get; set; }
public Dictionary<string,int> Location { get; set; }
}
和linq查询
AskQuestionFormView aqfv = new AskQuestionFormView();
var result = from c in db.Countries
为此,我尝试以json格式获取数据--我尝试使用以下web方法代码
格式:
[{name: 'May',data: [23]}, {name: 'June', data: [43]}, {name: 'July', data: [45]}]
代码:
try
{
var data = new Datas1().spsumdata().Select(s => new { name = s.Month, data = new int[] {s.data} }).ToArray();
return Newtonsoft.Json
我尝试根据此控制器操作创建产品详细信息(住宿详细信息)视图:
public ActionResult Detail(int id)
{
var detail = from cities in _db.Cities
join properties in _db.Properties on cities.CityId equals properties.CityId
join proplocations in _db.PropLocations on properties.Locatio
int[] checkeditemslist = new int[ChkList.Items.Count];//ChkList is Listbox name in xaml
ListBoxItem lt =((ListBoxItem)ChkList.Items[i]);
现在我得到了一个错误,一个类型为System.InvalidCastException的异常发生了,但是在用户代码中没有得到处理,无法将类型为CheckListItem的对象强制转换为类型System.Windows.Controls.ListBoxItem。
可能重复:
我有一个叫LeaveDetails.cs的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LeaveMgmt_14dec
{
[Serializable]
public class LeaveDetails
{
public string date;
public string leave_type;
}
}
然后创建一个LeaveDetails类对象的列表。
L