在 C# 4.0 中实现 IEnumerable
我有这个类:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您必须正确分配属性或使用构造函数:
或者
You have to either assign the properties correctly or use the constructor:
Or
更改您的初始化程序,您当前使用的语法适用于集合初始化程序,而不是对象初始化程序:
或者使用其他构造函数:
我认为您失败的地方是编译器足够聪明,可以处理类似的事情:
通过推断属性名称通过源类型的属性名称。请注意,您必须明确说明
StateInstanceDate
,因为InstanceDate
并不相同。Change your initialiser, the syntax you are currently using is for a collection initialiser, not an object initialiser:
Or use the other constructor:
I think where you falling over is that the compiler is smart enough to handle something like:
By infering the property names through the property names of the source type. Notice that you'd have to be explicit about
StateInstanceDate
becauseInstanceDate
is not the same.BrokenGlass 回答了您的详细初始化问题,但我想补充一件事有关您对一次性模式的使用。考虑这样的编码:
即使您的查询抛出异常,Using也会调用cs.Dispose()。
另一种想法 - 考虑像这样输入你的函数...
以防你使用不同类型的 IList 实现器,甚至更好:
只要你不需要 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:
Using will call cs.Dispose() even if your query throws.
One other thought - consider typing your function like so...
in case you use a different kind of IList-implementor, or even better:
which is even more flexible as long as you don't need IList-y stuff.
您在两种形式的初始化之间混合,您可以使用构造函数,如下所示:
或者在默认构造之后使用属性初始化,如下所示:
You are getting mixed halfway between two forms of initialisation, you could use the constructor, like so:
Or use property initialisation after the default construct like this:
或者
or
我认为这与这里的部分有关:
您可能想使用括号而不是方括号。
I think it has to do with the section here:
You probably want to use parens instead of brackets.
使用 System.Web.UI.WebControls;
使用 Trirand.Web.Mvc;
在此处输入代码
名称空间sample.Models
{
公共类个人模型
{
公共 int PersonID { 获取;放; }
公共字符串姓氏{获取;放; }
公共字符串名字{获取;放; }
公共字符串地址{获取;放; }
公共字符串城市{获取;放; }
公共 JQGrid OrdersGrid { 获取;放; }
}
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; }
}
您忘记将 () 添加到 new Detail 之后,然后您必须分配诸如 CId = p.CId 之类的属性...
You forgot to put () after new Detail and then you have to assign properties like CId = p.CId...