我正在针对ObjectContext编写LINQ查询。在LINQ to Entities中,我基本上需要做的是这样的(我知道这样不行,但我这样做是为了说明问题):
from c in context.Table
where key == int.Parse(c.KeyAsString)
order by int.Parse(c.KeyAsString)
select c
我不确定这是否可行... 有人知道方法吗?谢谢。
我正在针对ObjectContext编写LINQ查询。在LINQ to Entities中,我基本上需要做的是这样的(我知道这样不行,但我这样做是为了说明问题):
from c in context.Table
where key == int.Parse(c.KeyAsString)
order by int.Parse(c.KeyAsString)
select c
var keyString = key.ToString();
var query = from c in context.Table
where keyString == c.KeyAsString
order by c.KeyAsString
select c
如果您在使用order by时遇到问题,请使用像ToList()
或ToArray()
这样的方法将结果拉入内存,然后您就可以将其转换为int或使用自定义比较器。
KeyAsString
不能有前导零(例如,“2”应该在“01”之前),也不能有负数(例如,“0”应该在“-1”之前)。 - Slaumavar query = from c in context.Table
let IntOrder = context.Table.Take(1).Select(x => c.KeyAsString).Cast<int>().FirstOrDefault()
where IntOrder == key
orderby IntOrder
select c;