Solved

linq distinct values cast issue

Posted on 2014-04-14
5
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

624 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