1

I have a list created from a stored procedure using EF6.0

I have also created 3 classes

public class Resas
{
    public string todo{ get; set; }
    public string prop { get; set; }
    public string Code { get; set; }
    public string statusCode { get; set; }
    public string checkin { get; set; }
    public string checkout { get; set; }
    public List<profiles> profiles { get; set; }
}

public class profiles
{
    public string action { get; set; }
    public string id { get; set; }
    public string profileType { get; set; }
    public string title { get; set; }
    public string firstName { get; set; }
    public string middleName { get; set; }
    public string lastName { get; set; }
    public List<emailAddresses> emailAdresses { get; set; }
}

public class emailAddresses
{
    public string emailAddress { get; set; }
    public string emailAddress2 { get; set; }
}

I am doing a for-loop in the list and I need to get certain columns and put it in the array (I will put two, to keep it simple)

myEntities db = new myEntities();
List<rev_Result> revList = new List<rev_Result>();


revList.Clear();
revList = db.rev().ToList();

for (int i = 0; i < revList.Count(); i++)
{
    Resas resas = new Resas();
    profiles[] profiles = new profiles[1];

    resas.todo = revList[i].todo;
    resas.profiles[0].lastName = revList[i].lastName;
}

I am not familiar with C# as you can see from the psedo-code above.

I cannot figure out how to feed the Resas with data and then its Profile with data and then move to the next Resas entry.

Any help appreciated.

2 Answers 2

3

That's fairly simple using Linq:

Resas resas = new Resas();
resas.profiles = revList
    .Select(x => new profiles() { action = x.todo, lastName = x.lastName })
    .ToList();

What's happening here is: You loop through every entry in revList and get your wanted data structure (that's what Select is doing). x refers to the current entry in the loop, while the stuff to the right side of the arrow is you 'output': a new instance of your profiles class with the members assigned accordingly. The result of all of this is then converted to a list (before ToList(), think of it as a recipe to create the list) and assigned to resas.profiles.


By the way, a word on conventions: Usually, in C#, you would give your classes a name that starts with a capital letter. Also, your profiles class seems to contain data of exactly one profile, so a better name might be Profile. This also makes your data structure more clear, since List<profiles> seems to be a list of lists of profiles - but that's not what it actually is, is it?

Furthermore, Members generally start with a capital letter as well, so instead of action, lastName, you'd have: Action and LastName.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! What if I needed to put the .todo in the Resas class (I modified the code example). Also after your script, how can I reference the object? (I want toserialize it afterwards)
This depends on what exactly you want to store in todo. Could you edit your question to include a sample input and expected output?
1

You can try with Linq. This is the code that should solve your issue, but Resas class doesn't have action property:

List<Resas> ls = revList.Select(x => new Resas() { 
    action = x.todo, 
    profiles = new List<profiles>() { 
        new profiles { lastName = x.lastName }
    }
).ToList();

If you need to use action property of inprofiles` class:

List<Resas> ls = revList.Select(x => new Resas() { 
    profiles = new List<profiles>() { 
        new profiles { 
            action = x.todo,
            lastName = x.lastName
        }
    }
).ToList();

3 Comments

Thank you but I cannot understand how can I acces the class instance and its properties, after the linq execution.
Dear Erik, how big modification is needed in order to go one stage lower (to the emailAddresses class) and add the emailaddress as well? Thank you
Simple :-) after lastName = x.lastName add emailAdresses = new List<emailAdresses> { new emailAdresses { emailAddress = "", emailAddress2 = "" }}. It's the same of prev step for profiles :-)

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.