我有ObservableCollection<CustomerModel> Customers
,其中包含国家/地区字段。我想要做的是,创建一个PiePointModel类型的可观察集合。为了存储国家/地区名称和该国家/地区名称的出现次数。
所以我设置了ObservableCollection<PiePointModel> CountryRatioCollection
,其中PiePoint拥有名称和金额。
然后我尝试将该集合转换为我的客户,将其转换为包含所需值的字典:
CountryRatioCollection = new ObservableCollection<PiePointModel>();
CountryRatioCollection = Customers.GroupBy(i => i.Country).ToDictionary(g => g.Key, g => g.Count());
但是我得到一个错误,指出这不能被隐式转换:
Error 2 Cannot implicitly convert type 'System.Collections.Generic.Dictionary<string,int>' to 'System.Collections.ObjectModel.ObservableCollection<MongoDBApp.Models.PiePointModel>'
据我所知,这是因为Dictionary类型与我的PiePoint模型类不同。
有人可以提供有关查询和转换的建议吗?
这是PiePoint类供参考,它包含名称和数量:
public class PiePointModel
{
public string Name { get; set; }
public int Amount { get; set; }
}
这是包含国家/地区字段的CustomerModel:
public class CustomerModel
{
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("firstName")]
public string FirstName { get; set; }
[BsonElement("lastName")]
public string LastName { get; set; }
[BsonElement("email")]
public string Email { get; set; }
[BsonElement("address")]
public string Address { get; set; }
[BsonElement("country")]
public string Country { get; set; }
public override string ToString()
{
return Country;
}
}
答案 0 :(得分:1)