0

i have code like this :

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

Exception I got :

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

whst should be done ?

Product Material Class :

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

list.Select(x => x.ProductMaterial).Distinct().ToList() gives me array string though have converted to list , i want the result of type 'ProductMaterial'.

DharaPPatel
  • 12,035
  • 9
  • 31
  • 49

3 Answers3

0

Use this instead:

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

Or:

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

The only thing that is different is the type of the materials variable.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
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

Community
  • 1
  • 1
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
0

The query list.Select(x => x.ProductMaterial).Distinct().ToList(); returns List<string> not List<ProductMaterial>.

You can use List instead of List<ProductMaterial>. I am not sure about what you want.I could have given a more precise opinion if data type of list was specified.

Aneesh Mohan
  • 1,077
  • 8
  • 17
  • @DharaPPatel what is the data type of var list ? if var list is List then you can use **WHERE** instead of **SELECT** to query down distinct entries – Aneesh Mohan Jun 04 '13 at 12:18