Solved

linq distinct values cast issue

Posted on 2014-04-14
5
359 Views
Last Modified: 2016-02-16
Hi,
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

0
Comment
Question by:jackjohnson44
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39999885
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"));
0
 

Author Comment

by:jackjohnson44
ID: 40001599
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.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40001630
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))
).ToList();
0
 

Author Comment

by:jackjohnson44
ID: 40001673
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);
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40002041
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();
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question