gpt4 book ai didi

c# - 在 C# 4.0 中实现 IEnumerable

转载 作者:行者123 更新时间:2023-11-30 19:19:48 25 4
gpt4 key购买 nike

我有这门课:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com