1

I have repository class which contains a list with people who filled in a form on my website if they will attend on my party or not. I read values with the GetAllRespones and I add values to the list with AddResponse (via an interface)

Now I want to check if someone already filled in my form and if so I want to check if the value of WillAttend is changed and update it.

I can see what I did below here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PartyInvites.Abstract;

namespace PartyInvites.Models
{
public class GuestResponseRepository : IRepository

{
    private static List<GuestResponse> responses = new List<GuestResponse>();

    IEnumerable<GuestResponse> IRepository.GetAllResponses()
    {
        return responses;
    }

    bool IRepository.AddResponse(GuestResponse response)
    {
       bool exists = responses.Any(x => x.Email == response.Email);
        bool existsWillAttend = responses.Any(x => x.WillAttend == response.WillAttend);

        if (exists == true)
        {
            if (existsWillAttend == true)
            {
                return false;
            }

           var attend =  responses.Any(x => x.Email == response.Email && x.WillAttend == response.WillAttend);
           attend.WillAttend = response.WillAttend;
           return true;

        }

        responses.Add(response);
        return true;
    }
}
}

the problem is, i get a error message at "attend.WillAttend"

the error is: bool does not contain definition for WillAttend and has no extension method 'WillAttend' accepting a first argument of type bool could be found

Could anyone help me out fix my code? :)

2 Answers 2

7

The problem is here:

var attend = 
        responses.Any(x => x.Email == response.Email && x.WillAttend == response.WillAttend);

Any<>() returns bool. bool doen't have property WillAttend. If you want to get first response with x => x.Email == response.Email && x.WillAttend == response.WillAttend use First() (or FirstOrDefault() but in your case you always will have at least one element, so simply use First()):

var attend = responses.First(x => x.Email == response.Email && x.WillAttend != response.WillAttend);
attend.WillAttend = response.WillAttend;

If you want many responses with specified condition use Where():

var attend = responses.Where(x => x.Email == response.Email && x.WillAttend != response.WillAttend);

if (attend.Any())
{
    //do something
}

Also, you can make your method simpler:

bool IRepository.AddResponse(GuestResponse response)
{
    if (responses.Any(x => x.Email == response.Email)) //here
    {
        if (responses.Any(x => x.WillAttend != response.WillAttend)) //here
        {
            return false;
        }

        var attend = responses.First(x => x.Email == response.Email && x.WillAttend != response.WillAttend);
        attend.WillAttend = response.WillAttend;          
        return true;
    }

    responses.Add(response);
    return true;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the fast reply! Implemented the code you suggested, but for some reason var attend keeps being null. Any ideas about that?
@valheru, have you got NullReferenceException or seen value using debugger?
Fixed it with x.WillAttend != response.WillAttend as suggested by Tamás Szabó and it works fine now. Thank you very much with your help!
2

responses.Any(...) returns a bool value (whether responses contains a value you specified). You will have to actually get that value with

responses.First(<lambda expression you specified>)

for example and get WillAttend on that object.

5 Comments

Thanks that did the trick! To bad an other problem occurred after it
What was it? Maybe I can help.
var attend keeps being 'null'
That must be because there is no object that meets the criteria you specified with the lambda expression. I think you should write x.WillAttend != response.WillAttend since you are looking for a change in attendance.
Thanks a lot that was the problem! So stupid I didn't think about it myself :)

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.