Solved

C# Add List<>  to another List<>

Posted on 2016-08-09
1
81 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
[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
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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