• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

linq distinct values cast issue

I have a function that returns a collection of objects.  I call it two times and need to combine the lists that are returned, but only the new objects.  I want to remove the duplicates from the second list before combining them.  When I try this, I am getting an error

Error      58      Cannot implicitly convert type 'System.Collections.Generic.List<GridProductDefinitionListModel>' to 'System.Collections.ObjectModel.ReadOnlyCollection<GridProductDefinitionListModel>'      

productName = "LocationSelector";
columns = productDefsRepository.GetColumns(web, productName);
cols.AddRange(ColDefModelsFromColumns(columns, replacements));
productName = "Recommendations";
ReadOnlyCollection<GridProductDefinitionListModel> columns2;
columns2 = productDefsRepository.GetColumns(web, productName);

ReadOnlyCollection<GridProductDefinitionListModel> columnsCombined;
columnsCombined = columns2.Except(columns).ToList();

Open in new window

  • 3
  • 2
1 Solution
Kyle AbrahamsSenior .Net DeveloperCommented:
List<GridProductDefinitionListModel> columns2;
columns2 = productDefsRepository.GetColumns(web, productName).ToList();

Note that you can also just call a union:

productDefsRepository.GetColumns(web, "LocationSelector").Union(productDefsRepository.GetColumns(web, "Recommendations"));
jackjohnson44Author Commented:
Thanks.  I was actually not 100% accurate in my description.  I found that I just need to check one column, not the whole object.

I need all the objects in "column" and the objects in "column2" where column2.data does not exist in column.data.  I am still having issues on the cast.
Kyle AbrahamsSenior .Net DeveloperCommented:
use the select command for that:

var ColumnData = productDefsRepository.GetColumns(web, productName).Select(pn => pn.Column).Union(
(productDefsRepository.GetColumns(web, "Recommendations").Select(r => r.Column2))
jackjohnson44Author Commented:
Thanks, but I am trying to get the whole list of objects, not just the column itself.  I only want all items from the first list and the items in the second list, but filter out if a row in the second list.ColumnData exists in the first list.ColumnData.

Here is what I am trying to do, if the cast worked:

//this just gets the values on the first list so I can filter them out of the second
var columnDataValues = (from col in columns select col.ColumnData).ToList();

//this will give me an Ienumerable, but breaks on the cast.
ReadOnlyCollection<GridProductDefinitionListModel> columns3 = (ReadOnlyCollection<GridProductDefinitionListModel>)(from c in columns2 where !columnDataValues.Contains(c.ColumnData) select c);
Kyle AbrahamsSenior .Net DeveloperCommented:
Does it need to be a readonlycollection?

List<GridProductDefinitionListModel> columns3 = (from c in columns2 where !columnDataValues.Contains(c.ColumnData) select c).ToList();

or if you really want the ROC

columns3 = (from c in columns2 where !columnDataValues.Contains(c.ColumnData) select c).ToList().AsReadOnly();
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now