C# 在Linq where子句中使用带字符串的开关大小写
我尝试在LINQ查询中使用C# 在Linq where子句中使用带字符串的开关大小写,c#,sql,string,linq,where-clause,C#,Sql,String,Linq,Where Clause,我尝试在LINQ查询中使用where子句。当我使用数字值时,这很好,但是当我想要比较字符串值时,它在where子句中给出了一个错误 string StartBarcode = (from s in datamodel.Packages where s.RealBarcode == SelectedBarcode select s.StartBarcode
where
子句。当我使用数字值时,这很好,但是当我想要比较字符串
值时,它在where
子句中给出了一个错误
string StartBarcode = (from s in datamodel.Packages
where s.RealBarcode == SelectedBarcode
select s.StartBarcode).FirstOrDefault().ToString();
IQueryable<PageData> q = (from v in datamodel.VW_WaypointDetails
where (v.RealBarcode == null) ? StartBarcode : v.RealBarcode
select new PageData
{
Name = v.Name,
Surname = v.Surname,
Description = v.Description
}
string StartBarcode=(来自datamodel.Packages中的s
其中s.RealBarcode==SelectedBarcode
选择s.StartBarcode).FirstOrDefault().ToString();
IQueryable q=(来自datamodel.VW_航路点详细信息中的v
其中(v.RealBarcode==null)?起始条码:v.RealBarcode
选择新页面数据
{
Name=v.Name,
姓,
描述
}
错误是“无法将lambda表达式转换为类型“string”,因为它不是委托类型”(我添加了System.Linq
),并且无法将类型“string”隐式转换为“bool”。
如何将switch case
语句与string
值一起使用?
感谢您的所有回答。确保它会给您一个错误,因为LINQ where子句需要逻辑,其中结果应该是true或false 您的逻辑是(v.RealBarcode==null)?StartBarcode:v.RealBarcode,它只返回字符串 如果我们将你的linq翻译成英语,听起来会像: 选择条形码所在的一些值 听起来应该像: 选择一些条形码等于某个值的值 我认为你的逻辑应该是这样的
where v.RealBarcode == (v.RealBarcode == null) ? StartBarcode : v.RealBarcode
将失败,因为这应该是一个谓词,但您可以这样做:
IQueryAble<PageData> q = datamodel.VW_WaypointDetails.AsEnumerable()
.Select(wpd=> new PageData
{
Name = wpd.Name,
SurName = wpd.Surname,
Description = wpd.Description,
Barcode = wpd.Barcode == null ? StartBarCode : wpd.Barcode
}).AsQueryAble();
IQueryAble q=datamodel.VW_WaypointDetails.AsEnumerable()
.选择(wpd=>new PageData
{
Name=wpd.Name,
姓氏=wpd。姓氏,
Description=wpd.Description,
条码=wpd.Barcode==null?起始条码:wpd.Barcode
}).AsQueryAble();
抱歉,忘记了对.AsQueryAble()的调用。老实说,不确定这是否有效,但您可以尝试忽略对.AsEnumerable()的调用(实际上是从数据库获取数据)…我知道Linq to Entities不支持普通Linq作用域的某些功能,因此这可能不可能,您必须稍后创建实际对象。在
中,其中(v.RealBarcode==null)?StartBarcode:v.RealBarcode
计算为bool的表达式是预期的。这里发生的情况是,如果v.RealBarcode不是空的
,那么它返回的字符串应该是bool evaluted值我理解如何使用where子句,谢谢。但是当我尝试这样写时,它给出了错误的select用法错误。我必须使用IQueryable类型。。
IQueryAble<PageData> q = datamodel.VW_WaypointDetails.AsEnumerable()
.Select(wpd=> new PageData
{
Name = wpd.Name,
SurName = wpd.Surname,
Description = wpd.Description,
Barcode = wpd.Barcode == null ? StartBarCode : wpd.Barcode
}).AsQueryAble();