string connstr = "server=.;database=hospital;uid=sa;pwd=123";
DBHelper.SQLHelper db = new DBHelper.SQLHelper(connstr);
DataTable dt = db.ExcuteToDataTable("select * from employee");
DataTable td = db.ExcuteToDataTable("select * from hospital");
var data = dt.AsEnumerable();
var tada = td.AsEnumerable();
var result = from da in data
join ta in tada
on da.Field<string>("hptid") equals ta.Field<string>("hptid")
where da.Field<Int32>("id") < 500
select new
{
cardID=da.Field<Int32>("cardid"),
empName=da.Field<string>("empname"),
empSex=da.Field<string>("empsex"),
hptAddress=ta.Field<string>("hptname")
};
说什么 指定的转换无效
单步调试,查看
var data = dt.AsEnumerable();
var tada = td.AsEnumerable();
是否有数据
var tada = td.AsEnumerable();
这两个都有数据的
result的结果视图里面显示的 "指定的转换无效"
Message="指定的转换无效。"
Source="System.Data.DataSetExtensions"
StackTrace:
在 System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
在 System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
在 LinqDemo01.Db.<Excute>b__5(<>f__AnonymousType0`2 <>h__TransparentIdentifier0) 位置 D:\Projects\LINQ\LinqDemo01\Program.cs:行号 34
在 System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()
在 LinqDemo01.Db.Excute() 位置 D:\Projects\LINQ\LinqDemo01\Program.cs:行号 41
在 LinqDemo01.Program.Main(String[] args) 位置 D:\Projects\LINQ\LinqDemo01\Program.cs:行号 15
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
join ta in tada
on da.Field<string>("hptid") equals ta.Field<string>("hptid")
where da.Field<int>("id") < 500
select new
{
cardID=da.Field<int>("cardid")??0,
empName=da.Field<string>("empname")??"",
empSex=da.Field<string>("empsex")??"",
hptAddress=ta.Field<string>("hptname")??""
};
XDocument xd = XDocument.Load("name.xml");
XDocument dx = XDocument.Load("price.xml");
var result = from x in xd.Elements()
join d in dx.Elements()
on x.Element("pri") equals d.Element("pri")
select new
{
pre = d.Element("pric").Value,
name = x.Element("name").Value
};result 无结果。
<root>
<price>
<pric>01</pric>
<pri>01</pri>
</price><price>
<pric>02</pric>
<pri>02</pri>
</price><price>
<pric>03</pric>
<pri>03</pri>
</price><price>
<pric>04</pric>
<pri>04</pri>
</price>
</root>
name.xml<?xml version="1.0" encoding="gb2312" ?>
<root>
<price>
<pri>i01</pri>
<name>第一个</name>
</price><price>
<pri>02</pri>
<name>第二个</name>
</price>
<price>
<pri>03</pri>
<name>第三个</name>
</price>
<price>
<pri>04</pri>
<name>第四个</name>
</price></root>