Click here to Skip to main content
15,880,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the code:

C#
List<Model.Sys_Permission> sys_permissions = new BLL.Sys_Permission().GetAll();
            List<Model.Sys_Permission> sys_permissionsNew = new List<Model.Sys_Permission>();
            
            var tempPermission = new Model.Sys_Permission();
            permissionCodes.ForEach(r => {
                tempPermission = sys_permissions.Find(c => c.PermissionCode == r);
                sys_permissionsNew.Add(tempPermission);
            });
            return sys_permissionsNew;


the list permissionCodes is a List<string> include 5 string object : '1001','1002','1003','1004','1005' . 

the list sys_permissions  includes 5 Sys_Permission model  objects, the values of the permissionCode are also  :  '1001','1002','1003','1004','1005' . 

the issue is why it returns null when the find() method runs to the last one  ? 
the find() method can return '1001','1002','1003','1004' . but it returns null when it finds the '1005' ? 
so that the sys_permissionsNew only fetch four values without the last one valued '1005' . 

who can tell me what's wrong with it ? thanks . 
Posted
Comments
Agent__007 13-Jan-15 3:54am    
Not sure but are you sure its "1005", and not "1005 " or " 1005" or " 1005 " (notice the spaces)?

thanks you all . the issue is solved . cause the values of permissionCodes in the database are '1005 ' , but the value of 'sys_permissions' is '1005' in the databse .

it is my fault when I insert the permission codes into the db .
thanks original .
 
Share this answer
 
Comments
Agent__007 13-Jan-15 3:55am    
Oh snap! :D
If you look at the documentation: MSDN[^] it explains that:
Return Value
Type: T
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.
...
Important note
When searching a list containing value types, make sure the default value for the type does not satisfy the search predicate. Otherwise, there is no way to distinguish between a default value indicating that no match was found and a list element that happens to have the default value for the type. If the default value satisfies the search predicate, use the FindIndex method instead.

Basically, it returns the default value when it runs out of matches - for a reference type, that's null
 
Share this answer
 
Comments
carono 13-Jan-15 3:24am    
thanks . but the values of 'permissionCodes ' and values of permissionCode in list 'sys_permissions' are absolute matched . I debugged their values in the vs 2012 . but I always can get the last object .
carono 13-Jan-15 3:26am    
cause I want it to return me the all values which are matched . normally , it must return 5 objects . if there is something wrong with the conditions ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900