Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing IEnumerable in C# 4.0

I have this class:

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; }
}

and this how I am trying to access data using 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;
}

But I am getting this error,

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

Any help ?

like image 783
user1005310 Avatar asked Jun 22 '26 10:06

user1005310


1 Answers

You have to either assign the properties correctly or use the constructor:

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

Or

select new Detail 
{
  CId = p.CId, 
  StateInstanceDate = p.InstanceDate, 
  StateName = q.StateName 
}
like image 74
BrokenGlass Avatar answered Jun 25 '26 02:06

BrokenGlass