Add item at start

S

shapper

Hello,

I am creating a list of Role as follows:

IList<Role> roles = new List<Role>();
roles.Add(new Role { Type = RoleType.Administrator, Name =
"Administrador", Open = false });
...

After I add all roles I need to add a new role at the start of the
list.

Can I do this?

Thanks,
Miguel
 
S

shapper

Look at the Insert() method.

Yes, I have tried it as follows:

MyRoles = Role.List(null, true).Insert(0, new Role { Name =
"Administrator", Open = true, Type = null });

Role.List returns an IList<Role>

But I get the following error:
Cannot implicitly convert type 'void' to
'System.Collections.Generic.IList<MyApp.Role>'

Any idea why this is happening?

Thanks,
Miguel
 
S

Stanimir Stoyanov

The correct lines of code are

MyRoles = Role.List(null, true);
MyRoles.Insert(0, new Role { Name = "Administrator", Open = true, Type =
null });

You were trying to assign the result of the Insert method (or the lack
thereof) to MyRoles. Instead, you should assign the result of your
Role.List() method to your List reference and then just call Insert() on the
new instance.
--
Stanimir Stoyanov
http://stoyanoff.info

Look at the Insert() method.

Yes, I have tried it as follows:

MyRoles = Role.List(null, true).Insert(0, new Role { Name =
"Administrator", Open = true, Type = null });

Role.List returns an IList<Role>

But I get the following error:
Cannot implicitly convert type 'void' to
'System.Collections.Generic.IList<MyApp.Role>'

Any idea why this is happening?

Thanks,
Miguel
 
S

shapper

The correct lines of code are

MyRoles = Role.List(null, true);
MyRoles.Insert(0, new Role { Name = "Administrator", Open = true, Type =
null });

You were trying to assign the result of the Insert method (or the lack
thereof) to MyRoles. Instead, you should assign the result of your
Role.List() method to your List reference and then just call Insert() on the
new instance.
--
Stanimir Stoyanovhttp://stoyanoff.info





Yes, I have tried it as follows:

MyRoles = Role.List(null, true).Insert(0, new Role { Name =
"Administrator", Open = true, Type = null });

Role.List returns an IList<Role>

But I get the following error:
Cannot implicitly convert type 'void' to
'System.Collections.Generic.IList<MyApp.Role>'

Any idea why this is happening?

Thanks,
Miguel

Yes, I know ... but I was trying to do this in one code line ...
that's why I was trying this
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Generics 7
Dictionary 3
Intersection of Lists 5
List. Add, Remove. 1
Linq query 3
Pass empty string 4
Join 3
Update Object 5

Top