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;
}
}
}
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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With