0

I have a situation where I need to populate a list object by looping through a loop of objects, check against a condition, and if the condition is met, add the item to my list. Currently I cannot find an example of the syntax required to add to the list rather than clear it and repopulate it each time the loop iterates to the next item. Can somebody help?

List<ProfileRightsJSON> prf = new List<ProfileRightsJSON>();

try
{
    for (int i = 0; i < lstProfiles.Count; i++)
    {
        prf = (from p in _database.tblProfileRights
               where p.fkProfileID ==
               lstProfiles[i].ProfileID
               select new ProfileRightsJSON
               {
                   FunctionID = p.fkFunctionID,
                   UserTypeID = p.fkUserTypeID,
                   RecursiveRights = p.RecursiveRights
               }).ToList();
    }

1 Answer 1

2

It looks like you really want something like this:

var profileIds = lstProfiles.Select(x => x.ProfileID).ToList();

var prf = (from p in _database.tblProfileRights
           where profileIds.Contains(p.fkProfileID)
           select new ProfileRightsJSON
           {
               FunctionID = p.fkFunctionID,
               UserTypeID = p.fkUserTypeID,
               RecursiveRights = p.RecursiveRights
           }).ToList();
Sign up to request clarification or add additional context in comments.

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.