这是我的留言类:
public class SMessage
{
public int AccountId { get; set; }
public int MessageId { get; set; }
public string str{ get; set; }
public string Subject { get; set; }
public int sendByAccountId { get; set; }
[ForeignKey("sendByAccountId")]
public virtual AccountModels account { get; set; }
}
这是我的dto课程:
public class MessageInformation
{
public string Subject { get; set; }
public string str { get; set; }
}
这是我在存储库中的方法:
public List<MessageInformation> GetAllMessagesForUser(int accountId)
{
MessageInformation messages
using (SishanContext Context = new SishanContext())
{
messages = (from m in Context.SMessages.ToList()
where m.AccountId == accountId
select new MessageInformation
{
m.Subject,
m.str
}).ToList();
}
return messages;
}
但是,我有一个错误:
无法使用集合初始化程序初始化类型'sishan.Models.MessageInformation',因为它没有实现'System.Collections.IEnumerable'
答案 0 :(得分:2)
您应该在初始化程序中命名属性,如下所示:
...
select new MessageInformation {
Subject = m.Subject,
str = m.str
}
或者,您可以提供一个双参数构造函数,并将其命名为:
public class MessageInformation {
public string Subject { get; set; }
public string str { get; set; }
public MessageInformation(string subj, string str) {
Subject = subj;
this.str = str;
}
}
...
select new MessageInformation(m.Subject, m.str)