在 C# 4.0 中实现 IEnumerable

发布于 2024-12-14 15:13:42 字数 1011 浏览 3 评论 0原文

我有这个类:

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”

有任何帮助吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

后知后觉 2024-12-21 15:13:42

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

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

或者

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

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 
}
别靠近我心 2024-12-21 15:13:42

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

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 并不相同。

Change your initialiser, the syntax you are currently using is for a collection initialiser, not an object initialiser:

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

Or use the other constructor:

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

I think where you falling over is that the compiler is smart enough to handle something like:

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

By infering the property names through the property names of the source type. Notice that you'd have to be explicit about StateInstanceDate because InstanceDate is not the same.

终陌 2024-12-21 15:13:42

BrokenGlass 回答了您的详细初始化问题,但我想补充一件事有关您对一次性模式的使用。考虑这样的编码:

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

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

另一种想法 - 考虑像这样输入你的函数...

public IList<Detail> Getinfo()

以防你使用不同类型的 IList 实现器,甚至更好:

public IEnumerable<Detail> Getinfo()

只要你不需要 IList-y 的东西,它就更加灵活。

BrokenGlass answered your Detail-initialization question but I'd like to add one more thing about your use of the Disposable pattern. Consider coding like this:

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

Using will call cs.Dispose() even if your query throws.

One other thought - consider typing your function like so...

public IList<Detail> Getinfo()

in case you use a different kind of IList-implementor, or even better:

public IEnumerable<Detail> Getinfo()

which is even more flexible as long as you don't need IList-y stuff.

爱的故事 2024-12-21 15:13:42

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

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

或者在默认构造之后使用属性初始化,如下所示:

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

You are getting mixed halfway between two forms of initialisation, you could use the constructor, like so:

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

Or use property initialisation after the default construct like this:

new Detail { CId = p.CId, StateInstanceDate = p.InstanceDate, StateName = p.StateName }
月亮邮递员 2024-12-21 15:13:42
select new Detail(p.CId, p.InstanceDate, q.StateName);

或者

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

or

rselect new Detail 
{
    CId = p.CId, 
    StateInstanceDate = p.InstanceDate, 
    StateName = q.StateName 
};
本宫微胖 2024-12-21 15:13:42

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

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

您可能想使用括号而不是方括号。

I think it has to do with the section here:

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

You probably want to use parens instead of brackets.

从来不烧饼 2024-12-21 15:13:42

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

在此处输入代码

名称空间sample.Models
{
公共类个人模型
{
公共 int PersonID { 获取;放; }
公共字符串姓氏{获取;放; }
公共字符串名字{获取;放; }
公共字符串地址{获取;放; }
公共字符串城市{获取;放; }
公共 JQGrid OrdersGrid { 获取;放; }

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

}

}

using System.Web.UI.WebControls;
using Trirand.Web.Mvc;

enter code here

namespace sample.Models
{
public class PersonalModel
{
public int PersonID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public JQGrid OrdersGrid { get; set; }

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

}

}

凉墨 2024-12-21 15:13:42

您忘记将 () 添加到 new Detail 之后,然后您必须分配诸如 CId = p.CId 之类的属性...

You forgot to put () after new Detail and then you have to assign properties like CId = p.CId...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文