无法将类型'System.Collections.Generic.List <int>'隐式转换为'System.Collections.Generic.IEnumerable <model>'

时间:2017-05-25 04:01:08

标签: c# asp.net-mvc entity-framework

使用Select()时出现此错误

  

无法隐式转换类型'System.Collections.Generic.List&lt; int&gt;'到'System.Collections.Generic.IEnumerable&lt; OgrenciEvi.Areas.Message.Models.MessageModel&gt;'。

SiteContext db = new SiteContext();
[Route]
public ActionResult Index()
{
    var model = new ViewModel();
    int UserID = int.Parse(User.Identity.GetID());
    model.Messages = db.Message.Where(m => m.ReceiverID == UserID || m.SenderID == UserID).Select(m=>m.SenderUser.Name).ToList();

    return View(model);
}

我如何解决?

编辑:

public class MessageModel
{
    [Key]
    public int MessageID { get; set; }

    [Required(ErrorMessage = "Boş mesaj gönderilemez.")]
    public string MessageContent { get; set; }

    [Required]
    public int SenderID { get; set; }

    [Required]
    public int ReceiverID { get; set; }

    [Required]
    public DateTime SendingDate { get; set; }

    [Required]
    public bool IsSeen { get; set; }

    public virtual UserModel SenderUser { get; set; }
    public virtual UserModel ReceiverUser { get; set; }


    MessageModel() {
        SendingDate = DateTime.Now;
    }
}

的usermodel:

public class UserModel
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int UserID { get; set; }

    [Required (ErrorMessage ="İsim alanı boş geçilemez.")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Soyadı alanı boş geçilemez.")]
    public string Surname { get; set; }


    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                       @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                       @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                       ErrorMessage = "E-Posta adresi geçersiz.")]
    [DataType(DataType.EmailAddress)]
    public string Mail { get; set; }

    [DataType(DataType.Password)]
    public string Password { get; set; }

    [NotMapped] // Does not effect with your database
    public string ConfirmPassword { get; set; }

    public int Gender { get; set; }
    [RegularExpression("^[0-9]*$", ErrorMessage ="Telefon numarası geçersiz.")]
    public string PhoneNumber{ get; set; }
    public int SmokeStatus { get; set; }
    public DateTime RegistrationDate { get; set; }

    [HiddenInput(DisplayValue = false)]
    public string ReturnUrl{ get; set; }
    [Range(1900,2000, ErrorMessage ="Doğum yılı 1900 ile 2000 yılları arasında olmalı.")]
    [Required(ErrorMessage ="Yaş alanı boş geçilemez.")]
    public int BirthYear { get; set; }

    public string FacebookID { get; set; }
    public bool Manager { get; set; }

    public int AccessFacebook { get; set; }
    public int AccessInstagram { get; set; }
    public int AccessPhoneNumber { get; set; }
    public int AccessTwitter { get; set; }

    public List<LookingForaMateModel> LFMate { get; set; }
    public List<LookingForaHome> LFHome { get; set; }
    public List<AlbumModel> Album { get; set; }
    public List<PropertyTransferModel> PropertyTransfer{ get; set; }

    [ForeignKey("SenderID")]
    public List<MessageModel> SenderUser { get; set; }

    [ForeignKey("ReceiverID")]
    public List<MessageModel> ReceiverUser { get; set; }



    public UserModel() {
        RegistrationDate = DateTime.Now;
    }


}

我想为我的网站“直接留言”,设计看起来像facebook messenger,所以左侧有一个面板。我应该获取列表消息的发件人姓名和最后一条消息。但我无法做到:)

2 个答案:

答案 0 :(得分:0)

您仍然没有显示ViewModel类的代码。但假设model.Messages的类型为List<Message>,则以下内容应该有效:

    var model = new ViewModel();
    int UserID = int.Parse(User.Identity.GetID());
    model.Messages = db.Message.Where(m => m.ReceiverID == UserID || m.SenderID == UserID).ToList();

答案 1 :(得分:-1)

制作消息类型IEnumerable