Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Add List<>  to another List<>

Posted on 2016-08-09
1
Medium Priority
?
92 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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

670 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