1

I have following lists

List<AspNetUser> approverprofiles = db.AspNetUsers.Where(w => w.Subsidary_ID =="04").ToList();

List<AspNetRole> approverusers = db.AspNetRoles.Where(w => w.Name == "Approver").ToList();

approverprofiles is a AspNetUser LIST and approverusers is a AspNetRole LIST , how can I merge or add these two lists into one list ?

kez
  • 2,273
  • 9
  • 64
  • 123

4 Answers4

1

Instead of merging or adding, you can serialize it into one class as-

Public Class MergeClass {

  public List<AspNetUser> AspNetUsers {get;set;}
  public List<AspNetRole> AspNetRoles {get;set;}

}

Now-

var mergeList= new MergeClass {
  AspNetUsers= approverprofiles.ToList(),
  AspNetRoles=  approverusers.ToList()
}

Edit

As far as adding an object to the existing class is simple

Public Class MergeClass {

  public List<AspNetUser> AspNetUsers {get;set;}
  public List<AspNetRole> AspNetRoles {get;set;}
  public string MyObject {get;set;}
}

You can then use it-

  var mergeList= new MergeClass {
      AspNetUsers= approverprofiles.ToList(),
      AspNetRoles=  approverusers.ToList(),
      MyObject= "someString"
  }

Accessing the properties-

var aspNetUsersFromMergedList = MergeClass.AspNetUsers.Select(a=>a.Id))

As per you problem with loop , you can access a property like this-

foreach(var approvermail in MergeClass.AspNetUsers.ToList(){
   //do something
}
Manoz
  • 6,507
  • 13
  • 68
  • 114
  • but then how to add objects to this new `mergeList` ?? – kez Feb 05 '16 at 05:54
  • another question popup here once I use your approach. I have a loop like this `foreach (var approvermail in mergeList){` but here I'm getting this error `foreach statement cannot operate on variables of type 'project_namea.Models.MergeClass' because 'project_name.Models.MergeClass' does not contain a public definition for 'GetEnumerator'` – kez Feb 05 '16 at 06:06
  • @kez, have you given public access to `MergeClass` properties? – Manoz Feb 05 '16 at 06:13
  • `public class MergeClass {public List AspNetUsers { get; set; } public List AspNetRoles { get; set; }}` yes I defined it as like this , what else should I do ? – kez Feb 05 '16 at 06:25
  • pardon me you understood it wrong way, could be my explaining mistake, I want to add objects that getting from `List approverprofiles = db.AspNetUsers.Where(w => w.Subsidary_ID =="04").ToList();` and `List approverusers = db.AspNetRoles.Where(w => w.Name == "Approver").ToList();` – kez Feb 05 '16 at 06:36
  • let me know , whether you got my idea ? – kez Feb 05 '16 at 06:38
  • to add objects? where do you want to add the objects? to these two lists? – Manoz Feb 05 '16 at 06:39
  • pardon me again I meant results that I'm getting through this line `List approverprofiles = db.AspNetUsers.Where(w => w.Subsidary_ID =="04").ToList();` and this line `List approverusers = db.AspNetRoles.Where(w => w.Name == "Approver").ToList();` – kez Feb 05 '16 at 06:40
  • How to put those two results into this new list ? – kez Feb 05 '16 at 06:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102661/discussion-between-manoz-and-kez). – Manoz Feb 05 '16 at 06:43
0

Try

approverprofiles = approverprofiles .Concat(approverusers ).ToList();

Concat returns an IEnumerable<T> that is the two lists put together, it doesn't modify either existing list. Also, since it returns an IEnumerable, if you want to assign it to a variable that is List<T>, you'll have to call ToList() on the IEnumerable<T> that is returned.

or You can try

approverprofiles.AddRange(approverusers)
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
  • 1
    How can you? can you add or concat 2 different tables? can you give me reference please? – Manoz Feb 05 '16 at 05:38
0
List<AspNetUser> approverprofiles = db.AspNetUsers.Where(w => w.Subsidary_ID =="04").ToList();

List<AspNetRole> approverusers = db.AspNetRoles.Where(w => w.Name == "Approver").ToList();

approverprofiles.AddRange(approverusers);
0

Use below inbuilt function to add second list to first list.

approverprofiles.AddRange(approverusers);