Cannot convert from 'int' to 'System.Predicate

c# entity-framework linq

Question

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

}
1
2
2/21/2016 12:24:27 PM

Accepted Answer

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));
2
2/21/2016 12:25:16 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow