-2

I have a List A as below

var act= (from a in _db.Activities
                          where a.UserID == userid
                          select new SchoolDetail
                          {
                              SchoolName = a.School,
                              SchoolCode = a.SchoolID
                          }).Distinct().ToList(); 

Then I have another list List B

var sch= (from s in _db.Schools
                           where s.UserID == userid
                           select new SchoolDetail
                           {
                               SchoolName = s.SName,
                               SchoolCode = s.SCode
                           }).Distinct().ToList();  

Now I want to append List B to List A and I tried using AddRange -

var schools = act.AddRange(sch);  

But I am getting an error message on AddRange line -

Exceptions - Argument null exception
Cannot assign void to an implicitly-typed variable

What am I doing wrong and how can I fix it?

Thanks!

Kristy
  • 279
  • 6
  • 18
  • 2
    Did you google the error message? Please include links to your research results and explain specifically how they didn't help you. I just googled it myself, and the first page of results is the same question over and over again...I find it hard to believe it was easier and quicker to write up an SO question here than just simply googling it.... – rory.ap Mar 22 '17 at 14:41
  • 2
    In addition to what rory said, did you read the error? It's pretty self explanatory. – tnw Mar 22 '17 at 14:43
  • For details on exception see: https://msdn.microsoft.com/en-us/library/z883w3dc(v=vs.110).aspx i.e. your variable 'sch' is null and hence the exception. Add some null checks to avoid exception. – Syed Osama Maruf Mar 22 '17 at 14:49
  • @rory - I did google it and that's why I posted the question because i am unable to get an answer. All the google searches don;t relate to the scenario I am having. No one is that much free to post a question and ask help for if they can get the answers themselves. – Kristy Mar 22 '17 at 14:52
  • @tnw - I know the error is self explanatory. But people who ask questions here can be newbie as well. Anyways, thanks for the help! – Kristy Mar 22 '17 at 14:52
  • 1
    @Kristy -- I'd suggest taking a look at [ask]. – rory.ap Mar 22 '17 at 14:53

3 Answers3

5

Solved it using a Concat instead of AddRange

var schools = act.Concat(sch);  
Kristy
  • 279
  • 6
  • 18
0

Your particular issue derives from a null value. Your sch is either null or contains a null. Rather than utilize the AddRange, which will modify a collection, appending to the end of the other. You may want to utilize the Concat. This will create a separate instance of your collection, and attempt to join them while not modifying the data. So a null won't impede a concat.

Otherwise to resolve your specific issue, you'll want to perform a null check.

var school = ...;
var student = ...;

var sample = school.Concat(student);

var school = ...;
var student = ...;
var filter = school.Where(d => d != null);
var sample = student.AddRange(school);
Greg
  • 11,302
  • 2
  • 48
  • 79
-1

The function "AddRange" add the list sch to the list act, and is a void, it doesn't return anything. I guess what you want would be something like:

        List<...> schools = new List<...>(act);
        schools.AddRange(sch);