c# - 在 C# 4.0 中实现 IEnumerable

标签 c# .net .net-4.0 ienumerable linq-to-objects

我有这门课:

public class Detail
{
    public Detail() { }
    public Detail(Guid Id, DateTime InstanceDate, string Name)
    {
        CId = Id;
        StateInstanceDate = InstanceDate;
        StateName = Name;
    }

    public Guid CId { get; set; }
    public DateTime StateInstanceDate { get; set; }
    public string StateName { get; set; }
}

这就是我尝试使用 LINQ 访问数据的方式:

public List<Detail> Getinfo()
{
    CaseContext cs = new CaseContext();
    var query = (from p in cs.table1    
                join q in cs.table2  
                 on p.StateKey equals q.StateKey 
                 select new Detail
                 {
                     p.CId,
                     p.InstanceDate,
                     q.StateName
                 }).ToList<Detail>();

    cs.Dispose();
    return query;
}

但是我得到了这个错误,

Cannot initialize type 'Detail' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

有什么帮助吗?

最佳答案

您必须正确分配属性或使用构造函数:

select new Detail( p.CId, p.InstanceDate, q.StateName)

或者

select new Detail 
{
  CId = p.CId, 
  StateInstanceDate = p.InstanceDate, 
  StateName = q.StateName 
}

关于c# - 在 C# 4.0 中实现 IEnumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8066713/

相关文章:

c# - 在 AngularJS/WebApi 应用程序中使用 Windows 身份验证的 CORS 问题 - 仅适用于 IE

c# - 获取 IP 地址 WPF 和 C# 显示问题

asp.net - DotNet 5.0 Swagger 未在 Azure 应用服务中加载

.net - 即时修改 app.config

asp.net - 任何主机都支持 asp.net 4.0 - 理想情况下需要绿色托管

c# - SQLite CodeFirst 示例

c# - 如何在 C# 中创建 UTF8 文件

c# - 如何管理 .net 应用程序中的频繁数据访问?

c# - 为什么 EventWaitHandle.WaitAny 方法有 64 个句柄限制?

c# - 将数据绑定(bind)到组合框