StuiterSlurf
StuiterSlurf

Reputation: 2572

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

}

Upvotes: 6

Views: 34225

Answers (3)

Steve Parish
Steve Parish

Reputation: 1874

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.

Upvotes: 5

Peter_Szabo
Peter_Szabo

Reputation: 11

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

Upvotes: 1

kkyr
kkyr

Reputation: 3845

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

Upvotes: 8

Related Questions