Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不实现IEnumerable_C#_Entity Framework - Fatal编程技术网

C# 不实现IEnumerable

C# 不实现IEnumerable,c#,entity-framework,C#,Entity Framework,这是我的消息类: 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(

这是我的消息类:

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”您应该在初始值设定项中命名属性,如下所示:

...
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)

你为什么打电话来?托利斯特两次?您可能希望分析查询,以查看筛选器是否应用于客户端,而不是数据库中的Where子句是否未传递到数据库;我认为这些不能省略。@Servy我很肯定你可以省略它们。事实上,ReSharper经常缠着我在花括号初始值设定项之前加入空括号:尽管我不再使用ReSharper,但过去的使用使我仍然痴迷地删除那些多余的空括号。。。