使用 LINQ 根据另一列的值选择一列
我的问题与这里的问题类似:选择基于特定列另一列的值,但在 LINQ 中,使用 VS2010、C#、.Net 4.0 和 LINQ to Entities
这是我遇到问题的代码片段
var BillingNumbers = (from o in mycontext.MyOrders
where o.Status.Description.Trim().ToUpper().Equals("SHIPPED")
&& (o.ActualShipDate >= date1.Date && o.ActualShipDate < date2.Date)
select new
{BillingNumber = (
o.SiteID == "NYC"? o.NYCBillingNumber.Trim():
o.SiteID == "DAL" ? o.DallasBillingNumber.Trim(): "unknown")
});
foreach (var billingnumber in BillingNumbers)
{
sqlcmd = MyDatabase.GetStoredProcCommand("MyBillingNumberSP") as SqlCommand;
sqlcmd.CommandTimeout = Int32.Parse(Settings.Default.SQL_COMMAND_TIMEOUT.ToString());
MyDatabase.AddInParameter(sqlcmd, "@BillingNumber", DbType.String, billingnumber);
MyDatabase.AddOutParameter(sqlcmd, "@MyOutputParam", DbType.Boolean, 1);
LobjDS = MyDatabase.ExecuteDataSet(sqlcmd); //GETTING AN ERROR HERE
bool JobbingApplied = Convert.ToBoolean(MyDatabase.GetParameterValue(sqlcmd, "@MyOutputParam"));
Console.WriteLine(billingnumber);
}
: ExecuteDataSet 命令抛出 InvalidCastException 并显示以下消息:
无法将参数值从 <>f__AnonymousType0`1 转换为字符串。
出了什么问题,为什么这个匿名类型在尝试将其解释为字符串时会抛出错误?
My question is similar to the one here: Select a specific column based on another column's value but in LINQ, using VS2010, C#, .Net 4.0 and LINQ to Entities
Here's the snippet I'm having an issue with:
var BillingNumbers = (from o in mycontext.MyOrders
where o.Status.Description.Trim().ToUpper().Equals("SHIPPED")
&& (o.ActualShipDate >= date1.Date && o.ActualShipDate < date2.Date)
select new
{BillingNumber = (
o.SiteID == "NYC"? o.NYCBillingNumber.Trim():
o.SiteID == "DAL" ? o.DallasBillingNumber.Trim(): "unknown")
});
foreach (var billingnumber in BillingNumbers)
{
sqlcmd = MyDatabase.GetStoredProcCommand("MyBillingNumberSP") as SqlCommand;
sqlcmd.CommandTimeout = Int32.Parse(Settings.Default.SQL_COMMAND_TIMEOUT.ToString());
MyDatabase.AddInParameter(sqlcmd, "@BillingNumber", DbType.String, billingnumber);
MyDatabase.AddOutParameter(sqlcmd, "@MyOutputParam", DbType.Boolean, 1);
LobjDS = MyDatabase.ExecuteDataSet(sqlcmd); //GETTING AN ERROR HERE
bool JobbingApplied = Convert.ToBoolean(MyDatabase.GetParameterValue(sqlcmd, "@MyOutputParam"));
Console.WriteLine(billingnumber);
}
The ExecuteDataSet command throws an InvalidCastException with the message:
Failed to convert parameter value from a <>f__AnonymousType0`1 to a String.
What is wrong and why is this anonymous type throwing an error when attempted to interpret it as string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换
为
var billingnumber
是一种具有一个字符串属性BillingNumber
的匿名类型。您需要引用该属性,它不会自动转换为其第一个成员。另一种方法是替换
为
那么您根本不需要匿名类型。
Replace
with
var billingnumber
is a anonymous type with one string propertyBillingNumber
. You need to reference that property, it is not automatically convertible to its first member.An alternative is to replace
with
Then you don't need the anonymous type at all.