C# 从SQL创建LINQ

C# 从SQL创建LINQ,c#,linq,tsql,ado.net,linq-to-dataset,C#,Linq,Tsql,Ado.net,Linq To Dataset,我正在尝试从下面的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

我正在尝试从下面的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();
我得到以下错误

指定的强制转换无效

这些是以下变量中的值,希望这有助于捕捉错误

tblPMaster.pday = Jun 28 2011 12:00AM
Parameter Pday = 28/11/2011

我不知道我哪里做错了。有人能帮助您获得正确的LINQ查询吗?

将此转换为迄今为止的非字符串

tblPMaster.Field<string>("pday")
tblPMaster.Field(“pday”)

@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();
DataTable tblMaster=newdatatable();
DataColumn dc=newdatacolumn(“pday”,Type.GetType(“System.String”);
TBL主列添加(dc);
tblMaster.Rows.Add(新对象[]{“2011年11月28日12:00AM”});
tblMaster.Rows.Add(新对象[]{“Apr 27 2013 11:10PM”});
tblMaster.Rows.Add(新对象[]{“2011年7月18日12:00AM”});
tblMaster.Rows.Add(新对象[]{“Mar 19 2012 10:01PM”});
DateTime PDay=新的日期时间(2011,11,28);
//foreach(tblMaster.AsEnumerable()中的变量行)
//{
//Console.WriteLine(“{0}”,Convert.ToDateTime(行[0]);
//}
var qry=tblMaster.AsEnumerable()
其中(p=>Convert.ToDateTime(p.Field(“pday”))==pday);
//qry.Dump();
以上代码已经在LinqPad上进行了测试。工作也很好;)


干杯,Maciej

您在代码中遇到了什么具体问题?@Servy抱歉,我错过了错误详细信息,现在我已经添加了它。例外情况是告诉您您的一个类型是错误的。什么类型是
Pday
?@Jason,我已经尝试过查找,但无法找到它,我还在变量中添加了值,有正确的想法。你是说
tblPMaster.Field(“pday”)
然后呢?是的。我还不知道它是否有效。我不在自动取款机上。但请尝试一下此外你的问题有两种情况。这可能是您试图转换的转换或数据。例如(将双数据转换为日期时间)抛出错误转换无效hi@syverrodriguez,我尝试了您建议的tblPMaster.Field(“pday”)==datetime.Parse(pday)或tblPMaster.Field(“pday”)==pday,我得到以下错误“Operator==不能应用于Operat system.datetime和string。@javadotnetcoder。你试过用单等号吗?确保将两边都解析为datetime。但有时,当您试图分析到目前为止的字符串时,会发生其他错误。当日期为空或无效时。真让人头痛。
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();