在这个c#代码中,我需要将userName值从string转换为int类型。 有谁知道请帮帮我。我有一个错误作为编译错误“无法将lambda表达式转换为类型'int',因为它不是像这样的委托类型。
ShoppingCartPartRecord cartRecord = null;
try {
cartRecord = _shoppingCartRepository.Get(r => r.Username == userName);
}
catch (InvalidOperationException ex) {
if (ex.Message == "Sequence contains more than one element") {
var badCarts = _shoppingCartRepository.Table.Where(x => x.Username == userName);
foreach (var shoppingCartPartRecord in badCarts) {
_shoppingCartRepository.Delete(shoppingCartPartRecord);
}
}
}
提前谢谢。
答案 0 :(得分:1)
如果没有源存储库,我们只能猜测方法的作用。
根据您描述的错误,get函数需要索引到数组或整数主键,因此函数错误
您应该能够按如下方式更改代码以达到预期效果
ShoppingCartPartRecord cartRecord = null;
try {
cartRecord = _shoppingCartRepository.Table.Single(r => r.Username == userName);
}
catch (InvalidOperationException ex) {
if (ex.Message == "Sequence contains more than one element") {
var badCarts = _shoppingCartRepository.Table.Where(x => x.Username == userName);
foreach (var shoppingCartPartRecord in badCarts) {
_shoppingCartRepository.Delete(shoppingCartPartRecord);
}
}
}