Solved

linq distinct values cast issue

Posted on 2014-04-14
5
355 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 39

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 39

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 39

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now