从SQL创建LINQ
我想从后面的Sql创建Linq查询,但无法让它工作。从SQL创建LINQ
SQL
"select distinct(roomName) as RoomName, tblroomid as RoomId
from TblMaster,tblrooms
where tblrooms.tblroomid = TblPresentationMaster.tblroomid
and convert(datetime, PDay, 101)='" + Pday + "'";
LINQ
(from tblRoom in tblRooms.AsEnumerable()
join tblPMaster in tblMaster.AsEnumerable()
on tblRoom.Field<int>("tblroomid") equals tblPMaster.Field<int>("tblroomid")
where tblPMaster.Field<string>("pday") == Pday
select tblRoom.Field<string>("roomName")).Distinct();
如果我尝试运行它
foreach (var myReader in query)
{
}
我收到以下错误
指定的转换无效。
这些都是在下面的变量值,希望这有助于在捕获错误
tblPMaster.pday = Jun 28 2011 12:00AM
Parameter Pday = 28/11/2011
我不知道我做错了。有人可以帮助获得正确的LINQ查询吗?
@javadotnetcoder,感谢您的澄清。我想我找到了解决办法...
试试:
DataTable tblMaster = new DataTable();
DataColumn dc = new DataColumn("pday", Type.GetType("System.String"));
tblMaster.Columns.Add(dc);
tblMaster.Rows.Add(new Object[]{"Nov 28 2011 12:00AM"});
tblMaster.Rows.Add(new Object[]{"Apr 27 2013 11:10PM"});
tblMaster.Rows.Add(new Object[]{"Jul 18 2011 12:00AM"});
tblMaster.Rows.Add(new Object[]{"Mar 19 2012 10:01PM"});
DateTime PDay = new DateTime(2011,11,28);
//foreach(var row in tblMaster.AsEnumerable())
//{
// Console.WriteLine("{0}", Convert.ToDateTime(row[0]));
//}
var qry = tblMaster.AsEnumerable()
.Where(p=>Convert.ToDateTime(p.Field<string>("pday"))==PDay);
//qry.Dump();
上述代码在LinqPad进行了测试。也可以;
干杯,Maciej
感谢@Macieji它的工作原理 – javadotnetcoder
@javadotnetcoder,y你非常欢迎;) –
投这个至今没有串
tblPMaster.Field<string>("pday")
你是不是指'tblPMaster.Field
是的。我不知道,但如果它的工作。即时消息不在我的电脑atm。但请尝试一下。 :) –
另外。你的问题有两种情况。它要么是你正在施放的投射或是数据。例如(将一个双精度数据转换为一个日期时间)抛出的错误抛出无效 –
你有什么具体的代码问题? – Servy
@Servy对不起,我错过了错误的详细信息,现在我已经添加了它。 – javadotnetcoder
例外是告诉你,你的类型之一是错误的。 – Jason