6

I am getting this error: Cannot convert from 'int' to 'System.Predicate ServerHomeWork.Models.Organisation'

At the part of the the line OrgList.Add(Organisation.Find(UTO.OrganisationId); the property UTO.OrganisationId is an int but still it says something is wrong. Can anyone tell me what is going wrong over here?

the code is over here:

public virtual List<Organisation> Organisation
{
    get
    {
        List<UsersToOrganisations> UserToOrg = UsersToOrganisations.FindListOfOrganisations(UserId);
        List<Organisation> OrgList = new List<Models.Organisation>();

        if (UserToOrg.Count > 0)
            foreach (UsersToOrganisations UTO in UserToOrg)
                OrgList.Add(Organisation.Find(UTO.OrganisationId));
        else
            OrgList.Add(new Organisation("No organisation was found.", 0));


        return OrgList;
    }
} 

this is the UserToOrganisation class

class UsersToOrganisations
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int OrganisationId { get; set; }
    public bool MainOrganisation { get; set; }

    /// <summary>
    /// Constructor for entity framework
    /// </summary>
    public UsersToOrganisations() { }

    /// <summary>
    /// Constructor for creation
    /// </summary>
    /// <param name="UserId"></param>
    /// <param name="OrganisationId"></param>
    public UsersToOrganisations(int UserId, int OrganisationId, bool MainOrganisation)
    {
        this.UserId = UserId;
        this.OrganisationId = OrganisationId;
        this.MainOrganisation = MainOrganisation;
    }

    public class UsersToOrganisationsContext : DbContext
    {
        public DbSet<UsersToOrganisations> UserToOrganisation { get; set; }
    }

    /// <summary>
    /// Get the list of organisations of a single user
    /// </summary>
    /// <param name="UserId"></param>
    /// <returns></returns>
    public static List<UsersToOrganisations> FindListOfOrganisations(int UserId)
    {
        using (var context = new UsersToOrganisationsContext())
        {
            var organisation = (from uto in context.UserToOrganisation
                               where uto.UserId == UserId
                                select uto);
            return organisation.ToList();
        }
    }

    /// <summary>
    /// Get the main organisation of a user
    /// </summary>
    /// <param name="UserId"></param>
    /// <returns></returns>
    public static UsersToOrganisations FindMainOrganisation(int UserId)
    {
        using (var context = new UsersToOrganisationsContext())
        {
            var UserToOrganisation = context.UserToOrganisation
                  .Where(uto => uto.UserId == UserId && uto.MainOrganisation == true)
                  .SingleOrDefault();

            return UserToOrganisation; 
        }
    }

}
Richard
  • 29,854
  • 11
  • 77
  • 120
StuiterSlurf
  • 2,464
  • 4
  • 34
  • 55

3 Answers3

8

Find takes a predicate as a parameter. All you are supplying is an int.

Try the following:

OrgList.Add(Organisation.Find(u => u.OrganisationId == UTO.OrganisationId));
Idos
  • 15,053
  • 14
  • 60
  • 75
kkyr
  • 3,785
  • 3
  • 29
  • 59
5

I found this question because I was using List.Find() instead of List.Contains(). Switching to using List.Contains() will work with just an int so no need for the predicate.

Steve Parish
  • 1,824
  • 2
  • 14
  • 12
1

The problem is in your lambda expression in OrgList.Add(Organisation.Find(UTO.OrganisationId));

You want to do something like OrgList.Add(Organisation.Find(item => item.OrganisationId == UTO.OrganisationId));

For more information: List(T).Find at MSDN

Peter_Szabo
  • 11
  • 1
  • 4