Solved

C# Add List<>  to another List<>

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System.Security 2 27
Alert on Server memory 2 24
VB.Net/LINQ: ".Contains" condition not finding existing result in MSSQL 16 36
Calculate number of nights between two dates 5 28
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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