0

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.

4
  • Nvmd, i misread your question. Could you post the code of your repository? Commented Apr 4, 2012 at 8:45
  • 1
    What is the type of _shoppingCartRepository, I'm guessing the Get method requires an int. Do you need _shoppingCartRepository.Table.First(r=>r.Username == userName)? Commented Apr 4, 2012 at 8:47
  • What is Get ?? is it a method of _shoppingCartRepository ??? Commented Apr 4, 2012 at 8:50
  • Yes I need to assign the int method to Get method. But this is not working. Commented Apr 4, 2012 at 8:53

1 Answer 1

1

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); 
            } 
        } 
    } 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.