Reputation: 143
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? :)
Upvotes: 1
Views: 455
Reputation: 12201
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;
}
Upvotes: 7
Reputation: 1418
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.
Upvotes: 2