thomsan
thomsan

Reputation: 483

How to add object to in to List

I have following code,

public List<ActiveViewUser> GetAllActiveUsers(int branchId){

List<ActiveViewUser> status = new List<ActiveViewUser>();
DataSet ds = DataAccessManager.ExecuteStoredProcedure("spGetAllActiveUsers", parameters);
// add an all user option to the list
ActiveViewUser allusr = new ActiveViewUser();
List<ActiveViewUser> allActiveusers = new List<ActiveViewUser>();
allusr.UserId = -1;
allusr.UserName = "AllUsers";
allusr.FirstName = "All";
status.Add(allusr);


foreach (DataRow dr in ds.Tables[0].AsEnumerable())
{
    ActiveViewUser usr = new ActiveViewUser();

    usr.UserId = dr["UserId"].ToString().Length == 0 
      ? 0 
      : Convert.ToInt32(dr["UserId"].ToString());

    usr.UserName = dr["UserName"].ToString();
    usr.FirstName = dr["FirstName"].ToString();
    allActiveusers.Add(usr);
}
var newli = allActiveusers.OrderBy(x => x.FirstName).ToList();
status.Add(newli); //Error occurred in this line

}

As per the above code, I need to first insert All as the first index and, other all active users need insert after the that order by user's FirstName. So i tried above query. its returns following error.

cannot convert from 'System.Collections.Generic.List<Lib.DataView.ActiveViewUser>' to 'Lib.DataView.ActiveViewUser'. What did i do wrong here. how can I fix this?

Upvotes: 0

Views: 75

Answers (2)

Steve
Steve

Reputation: 216353

Add requires a single element of the list type, now the code is trying to add a whole list (newli) instead of each single objects of that list.

This should insert, in the status list, all the elements of the newli list

status.AddRange(newli); 

The List<T> class has also an InsertTo method that allows to insert an element to a specific position. So you could even simplify a bit the code with

status = new List<ActiveUser>();
foreach (DataRow dr in ds.Tables[0].AsEnumerable())
{
    ActiveViewUser usr = new ActiveViewUser();
    usr.UserId = dr["UserId"].ToString().Length == 0 ? 0 : Convert.ToInt32(dr["UserId"].ToString());
    usr.UserName = dr["UserName"].ToString();
    usr.FirstName = dr["FirstName"].ToString();

    // Add directly to the status list....
    status.Add(usr);
}
// Order the status list and reassign the result to the same list
status = status.OrderBy(x => x.FirstName).ToList();

// Finally insert at position 0 the "AllUsers" user.
status.InsertTo(0, new ActiveUser{UserId=-1, UserName="AllUsers", FirstName="All"}
return status;

Upvotes: 3

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

You can try materialize (i.e. create) status with a help of Linq from top and body records:

// Top record(s)
var allusr = new ActiveViewUser[] {
  new ActiveViewUser() {
    UserId    = -1,
    UserName  = "AllUsers",
    FirstName = "All",
  } 
};

// Body records
var newli = DataAccessManager
  .ExecuteStoredProcedure("spGetAllActiveUsers", parameters)
  .Tables[0]
  .AsEnumerable()
  .Select(dr => new ActiveViewUser() {
     UserId    = dr["UserId"] == DBNull.Value ? 0 : Convert.ToInt32(dr["UserId"]), 
     UserName  = Convert.ToString(dr["UserName"]),
     FirstName = Convert.ToString(dr["FirstName"]),
   })
  .OrderBy(x => x.FirstName);

// Top and Body combined:
List<ActiveViewUser> status = allusr
  .Concat(newli)
  .ToList(); 

Upvotes: 1

Related Questions