Solved

linq distinct values cast issue

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Inserting LocalDB Table to SQL Server C# 3 51
Fastest way to get file count in MS Server 2012 3 38
C# DateTime Help 6 47
Disable the weekends on datepicker control 6 52
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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