2016-02-21 105 views
1

我收到此錯誤:無法從轉換‘詮釋’到‘System.Predicate ServerHomeWork.Models.Organisation’無法從「詮釋」轉換爲「System.Predicate

在行OrgList的一部分。加入(Organisation.Find(UTO.OrganisationId); 財產UTO.OrganisationId是int但仍說,什麼是錯誰能告訴我是怎麼回事錯在這裏

的代碼是在這裏:

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

這是UserToOrganisation類

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

} 
+0

AREN '你錯過了關閉'''在那裏? – Idos

+0

對不起,我做到了。但Visual Studio給出的消息仍然是一樣的。 – StuiterSlurf

回答

1

Find將謂詞作爲參數。你所提供的只是一個int。

嘗試以下操作:

OrgList.Add(Organisation.Find(u => u.OrganisationId == UTO.OrganisationId)); 
+0

謝謝你做到了。但究竟是一個謂詞? – StuiterSlurf

+0

http://stackoverflow.com/questions/1710301/what-is-a-predicate-in-c – kskyriacou

0

的問題是在你的lambda表達式中 OrgList.Add(Organisation.Find(UTO.OrganisationId));

你想要做這樣的事情 OrgList.Add(Organisation.Find(item => item.OrganisationId == UTO.OrganisationId));

欲瞭解更多信息:List(T).Find at MSDN

相關問題