Solved

C# Add List<>  to another List<>

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

18 Experts available now in Live!

Get 1:1 Help Now