0

I have a simple way to put 2 known profiles in my profileArray list as shown below:

Parameters params = new Parameters();
params.plist = new Plist();
params.plist.profileArray = new[]
        {
            new Profile{name = "john", age = 12, country = "USA"},
            new Profile{name = "Brad", age = 20, country = "USA"}
        };

Now i have a

List<Profiles> UserProfiles

which has a bunch of profiles in it.

How do i add this list to params.plist.profileArray?

Any help is appreciated.

Thanks.

This is what is in UserProfile:

List<Profiles> UserProfiles 
foreach(Profiles userProfile in UserProfiles)
{
string name = userProfile.Name;
string age = userProfile.Age;
string country = userProfile.Country;
string sex = userProfile.Sex;
string isMarried = userProfile.IsMarried;
}
5
  • 2
    Array is fixed-size. You cannot add items to it. You can allocate new, bigger one and make plist.profileArray reference that new array. Commented Mar 3, 2014 at 22:54
  • I was not too sure of the title. If it does not sound correct please can someone correct it. Thanks Commented Mar 3, 2014 at 22:55
  • Can you suggest a way or any example to do that. Thanks.. Commented Mar 3, 2014 at 22:56
  • Please put that in the answers so that if it works for me i can mark it. Commented Mar 3, 2014 at 22:56
  • I think you should use more appropriate names for your variables.params is a speacial keyword. Commented Mar 3, 2014 at 23:05

4 Answers 4

3

You can use Enumerable.ToArray:

params.plist.profileArray = UserProfiles.ToArray();

If you want to add the list to the array, an array cannot be modified, you have to create a new one, for example by using Enumerable.Concat:

var newProfile = params.plist.profileArray.Concat(UserProfiles);
params.plist.profileArray = newProfile.ToArray();

Since these are two different classes with similar properties:

var profiles = UserProfiles
   .Select(up => new Profile{name = up.Name, age = up.Age, country = up.Country});
var newProfile = params.plist.profileArray.Concat(profiles);
params.plist.profileArray = newProfile.ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

Please have a look at my edit. Maybe i was not too clear before. Thanks
If you know the type of the the data, why not use a List instead of an array? Since you are actually converting it from one to the other. Also, how does this not work as you expected? It seems fine.
1

Try this:

params.plist.profileArray = UserProfiles.ToArray();

How about this?

params.plist.profileArray =
    UserProfiles
        .Select(up => new
        {
            name = up.Name,
            age = up.Age,
            country = up.Country,
        })
        .ToArray();

1 Comment

I have a lot of other values in user profiles along with name, age and country. I don't think it will work
0

Try

params.plist.profileArray = params
                            .plist
                            .profileArray
                            .Concat( UserProfiles )
                            .ToArray()
                            ;

Comments

0

Since you're using an array (and arrays in C# are of fixed size), you'l need to create a new array with the combined data.

There are several ways to do this, the simplest would probably be something like this:

var newList = new List<Profile>();
newList.AddRange(params.plist.profileArray);
newList.AddRange(UserProfiles);

params.plist.profileArray = newList.ToArray();

If can you change the implementation of Plist, I would recommend changing the array to a List<Profile>. Then the code would look like this instead:

params.plist.profileArray.AddRange(UserProfiles);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.