在C#4.0中实现IEnumerable

时间:2011-11-09 14:47:57

标签: 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;
}

但是我收到了这个错误,

  

无法使用集合初始值设定项初始化类型'Detail',因为它没有实现'System.Collections.IEnumerable'

有任何帮助吗?

8 个答案:

答案 0 :(得分:8)

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

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

或者

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

答案 1 :(得分:3)

更改初始化程序,您当前使用的语法是集合初始化程序,而不是对象初始化程序:

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

或使用其他构造函数:

new Detail(p.CId, p.StateInstanceDate, p.StateName);

我认为你失败的地方是编译器足够聪明,可以处理类似的事情:

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

通过源类型的属性名称推断属性名称。请注意,您必须明确StateInstanceDate,因为InstanceDate不一样。

答案 2 :(得分:0)

你在两种初始化形式中间混合,你可以使用构造函数,如下所示:

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

或者在默认构造之后使用属性初始化:

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

我认为这与这里的部分有关:

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

您可能希望使用parens而不是括号。

答案 5 :(得分:0)

BrokenGlass回答了你的详细初始化问题,但我还想补充一点关于你使用Disposable模式的事情。考虑这样编码:

public List<Detail> Getinfo()    
{  
    using (CaseContext cs = new CaseContext())
    {
        return (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(); 
    }
}

即使您的查询被抛出,使用也会调用cs.Dispose()。

另一个想法 - 考虑输入你的功能......

public IList<Detail> Getinfo()

如果你使用不同类型的IList-implementor,甚至更好:

public IEnumerable<Detail> Getinfo()

只要你不需要IList-y,它就更灵活。

答案 6 :(得分:0)

使用System.Web.UI.WebControls; 使用Trirand.Web.Mvc;

在这里输入代码

namespace sample.Models {     公共类PersonalModel     {         public int PersonID {get;组; }         public string LastName {get;组; }         public string FirstName {get;组; }         public string Address {get;组; }         public string City {get;组; }         public JQGrid OrdersGrid {get;组; }

    public PersonalModel()
    {            

        OrdersGrid = new JQGrid
                         {
                             Columns = new System.Collections.List()
                            {
                                 new JQGridColumn { DataField = "PersonId", 
                                                    // always set PrimaryKey for Add,Edit,Delete operations
                                                    // if not set, the first column will be assumed as primary key
                                                    PrimaryKey = true,
                                                    Editable = false,
                                                    Width = 50 },                                    
                                 new JQGridColumn { DataField = "FirstName", 
                                                    Editable = true,
                                                    Width = 100 },
                                 new JQGridColumn { DataField = "LastName",                                                         
                                                    Editable = true,
                                                    Width = 100, 
                                                    },
                                 new JQGridColumn { DataField = "Address", 
                                                    Editable = true,
                                                    Width = 75 },
                                 new JQGridColumn { DataField = "City",
                                                    Editable =  true
                                                  }                                     
                             },
                             Width = Unit.Pixel(640),
                             Height = Unit.Percentage(100)
                         };

        OrdersGrid.ToolBarSettings.ShowRefreshButton = true;            
    }

}

}

答案 7 :(得分:-1)

你忘记在新细节之后放()然后你必须分配像CId = p.CId ......

这样的属性