0

我有这样的代码:

  var list = _appData.GetAdvanceSearchData(Convert.ToInt16(collection["Product"])).ToList();
   List<ProductMaterial> materials = list.Select(x => x.ProductMaterial).Distinct().ToList();

我得到的例外:

Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<Crescent.LinqModel.ProductMaterial>' 

应该做什么?

产品材质等级:

 public class ProductMaterial
{
    public string ProductMaterials {get;set;}
}

list.Select(x => x.ProductMaterial).Distinct().ToList() 给了我数组字符串虽然已经转换为 list ,但我想要'ProductMaterial'类型的结果。

4

3 回答 3

0

I guess you want DistinctBy

List<ProductMaterial> materials =
    list.DistinctBy(x => x.ProductMaterial).ToList();

You can use that extension method or search for some other implementation here

于 2013-06-04T09:59:36.230 回答
0

改用这个:

var materials = list.Select(x => x.ProductMaterial).Distinct().ToList();

或者:

List<string> materials = list.Select(x => x.ProductMaterial).Distinct().ToList();

唯一不同的是materials变量的类型。

于 2013-06-04T09:57:06.073 回答
0

查询list.Select(x => x.ProductMaterial).Distinct().ToList();返回List<string>not List<ProductMaterial>

您可以使用 List 而不是List<ProductMaterial>. 我不确定你想要什么。如果指定了列表的数据类型,我本可以给出更准确的意见。

于 2013-06-04T10:11:24.360 回答