Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert from 'int' to 'System.Predicate

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

}
like image 771
StuiterSlurf Avatar asked Dec 24 '22 09:12

StuiterSlurf


2 Answers

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));
like image 104
kkyr Avatar answered Dec 27 '22 19:12

kkyr


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.

like image 45
Steve Parish Avatar answered Dec 27 '22 18:12

Steve Parish