我试图在LINQ的帮助下将这组数据转换为对象,但是我收到了这个错误:
无法为隐式类型变量
指定void
我可以理解我必须使用Select()
功能,但我无法弄清楚如何做到这一点。
数据集:
[Zip] [Fips] [County] [StateAbbr] [StateName]
90210 12345 County1 CA California
90210 12346 County2 CA California
90210 12347 County3 CA California
90210 12348 County4 CA California
代码段:
var zipInfo = results.ForEach(z => new ZipInfoEntity{
Zip = z.Zip,
StateName = z.StateName,
StateAbbr = z.StateAbbr,
FipsCountyInfo = new List<FipsCountyInfoEntity>{
new FipsCountyInfoEntity {
Fips = z.Fips,
County = z.County
}
}
});
public ZipInfoEntity
{
public string Zip {get;set;}
public string StateName {get;set;}
public string StateAbbr {get;set;}
public IList<FipsCountyInfoEntity> FipsCountyInfo {get;set;}
}
public FipsCountyInfoEntity
{
public string Fips {get;set;}
public string County {get;set;}
}
我最终想要实现的是ZipInfoEntity的一个对象。 如果您将看到我的DataSet,它对所有行都有相同的Zip,StateAbbr和StateName。 County和Fips正在发生变化。
所以我的最终目标应该是
{
Zip: "90210",
StateAbbr: "CA",
StateName: "California",
FipsCountyInfo: [
{Fips: "12345", County: "County1"},
{Fips: "12346", County: "County2"},
...]
}
答案 0 :(得分:3)
首先,ForEach
会返回void
,并且您尝试将其分配给某些您无法在C#中执行的变量。
首先,你应该有类似的东西:
var zipInfo = results.Select(z => new ZipInfoEntity{
Zip = z.Zip,
StateName = z.StateName,
StateAbbr = z.StateAbbr,
在这里,您尝试将FipsCountyInfoEntity
类型的对象分配给FipsCountyInfoEntity
(IList<FipsCountyInfoEntity>
)的集合。而你应该分配新的集合:
FipsCountyInfo = new List<FipsCountyInfoEntity>(new FipsCountyInfo[] {
new FipsCountyInfoEntity{
Fips = z.Fips,
County = z.County
}
}
});
或者将该属性更改为FipsCountyInfoEntity
类型并按原样使用它:
FipsCountyInfo = new FipsCountyInfoEntity{
Fips = z.Fips,
County = z.County
}
});
编辑:
如注释中所指定的,要仅检索一个元素,您最后可以使用FirstOrDefault()
Linq方法,例如。 :
var zipInfo = results.Select(z => new ZipInfoEntity{
// ... same code as above
}).FirstOrDefault();
或者:
var zipInfo = results.FirstOrDefault(z => new ZipInfoEntity{
// ... same code as above
return true; // needs to return true on object you want to return
});
EDIT2:
要只获得一个带有FipsCountyInfoEntity
集合的元素,我建议做类似的事情:
var zipInfo = results.Select(e => new ZipInfoEntity
{
Zip = z.Zip,
StateName = z.StateName,
StateAbbr = z.StateAbbr,
FipsCountyInfo = results.Select(z => new FipsCountyInfoEntity
{
Fips = z.Fips,
County = z.County
}
}).FirstOrDefault();
或使用GroupBy
:
var zipInfo = results.GroupBy(e => new { e.Zip, e.StateName, e.StateAbbr }).Select(e => new ZipInfoEntity
{
Zip = e.Key.Zip,
StateName = e.Key.StateName,
StateAbbr = e.Key.StateAbbr,
FipsCountyInfo = e.Select(c => new FipsCountyInfoEntity
{
Fips = c.Fips,
County = c.County
}).ToList()
});
答案 1 :(得分:1)
FipsCountyInfo
中的ZipInfoEntity
属性应该像这样声明。
public ZipInfoEntity
{
public string Zip {get;set;}
public string StateName {get;set;}
public string StateAbbr {get;set;}
public FipsCountyInfoEntity FipsCountyInfo {get;set;} // <-- use class, not IList
}
答案 2 :(得分:1)
您需要正确设置FipsCountryInfo列表。 FipsCountyInfo是FipsCountyInfoEntity的列表,您尝试将FipsCountyInfoEntity对象分配给列表。
您需要替换此代码:
FipsCountyInfo = new FipsCountyInfoEntity{
Fips = z.Fips,
County = z.County
}
有了这个:
FipsCountyInfo = new List<FipsCountyInfoEntity>(){ new FipsCountyInfoEntity{
Fips = z.Fips,
County = z.County
}}
答案 3 :(得分:0)
这应该是你:
var output = new ZipInfoEntity
{
Zip = results.First().Zip,
StateName = results.First().StateName,
StateAbbr = results.First().StateAbbr,
FipsCountyInfo = new List<FipsCountyInfoEntity>(
results.Select(z=>
new FipsCountyInfoEntity {
Fips = z.Fips,
County = z.County
}
)
)
}
results.First()
只获取列表中的第一项。我们使用它,因为我们可以从第一个对象获得前三个字段。然后在创建FipsCountyInfo
列表时,我遍历结果集,为每个条目构建一个对象。
应该注意,这假设Zip
,StateName
和StateAbbr
在所有行中都是相同的。如果它们不是(即您在结果集中包含多个zip的详细信息),那么这将产生意外结果,并且您希望使用group by来从中获取IEnumerable<ZipInfoEntity>
结果