Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Add List<>  to another List<>

Posted on 2016-08-09
1
Medium Priority
?
108 Views
Last Modified: 2016-08-09
So, I have a model.  I was using the below code.  I'm  adding data from a collection to another.  It was replacing it but I need to add it.
var data = (from mt in myObj
			where mt.Description == desc
			select mt).ToList();

if (data?.Count > 0)
{
	newModel = (from d in data    //Need to Add - This is replacing
			       select new otherModel()
				{
					Description = d.Description,
					Name = d.Name,
				}).ToList();
}

Open in new window

I tried below
	newModel.Add(
                               (from d in data    //Need to Add - This is replacing
			       select new otherModel()
				{
					Description = d.Description,
					Name = d.Name,
				}).ToList();
				);

Open in new window

But I'm getting the below error.
'System.Collections.Generic.List<DefinedModel>' to 'DefinedModel'
0
Comment
Question by:CipherIS
1 Comment
 
LVL 1

Accepted Solution

by:
CipherIS earned 0 total points
ID: 41749881
Solution is below
newModel.AddRange(
                               (from d in data    //Need to Add - This is replacing
			       select new otherModel()
				{
					Description = d.Description,
					Name = d.Name,
				}).ToList();
				);

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

916 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