0

在这个 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);
                }
            }
        }

先感谢您。

4

1 回答 1

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); 
            } 
        } 
    } 
于 2012-04-04T09:30:59.593 回答