Leo
Leo

Reputation: 495

Cannot convert lambda expression to type 'int' because it is not a delegate type

In this c# code I need to convert the userName value from string to int type. Is anyone know please help me. I have got error as a compilation error "Cannot convert lambda expression to type 'int' because it is not a delegate type" like this.

        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);
                }
            }
        }

Thank you in advance.

Upvotes: 0

Views: 1922

Answers (1)

Bob Vale
Bob Vale

Reputation: 18474

Without the source to your repository we can only guess at what the methods do.

From the errors you are describing the get function expects either an index into an array or an integer primary key and so is the wrong function

You should be able to change the code as follows to achieve the desired effect

   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); 
            } 
        } 
    } 

Upvotes: 1

Related Questions