0

I need to Use array.contains method with two parameter that one off them is int?. how can i use?

  string[] role = Roles.GetRolesForUser(Membership.GetUser().UserName)
                       .ToArray();
  int[] ProcessId = biz.Context.tblRoleProcess
                    .Where(x => (role.Contains(x.Role) || 
                            role.Contains("Administrators")) && 
                            x.Measure == true)
                    .Select(x => x.ProcessId).ToArray();

return GetQuery(filterExpression, Columns)
                    .Where(x => ProcessId.Contains(
                            {x.ProcessID,x.ParentProcessID}))
                     .OrderBy<Models.tblProcess>(sortExpression)
                     .Skip(startRowIndex)
                     .Take(maximumRows);
1
  • (Read and post error messages.) Commented Apr 13, 2014 at 9:28

2 Answers 2

2

Try this:

return GetQuery(filterExpression, Columns)
            .Where(x => ProcessId.Contains(x.ProcessID)
                             || ProcessId.Contains(x.ParentProcessID))
            .OrderBy<Models.tblProcess>(sortExpression)
            .Skip(startRowIndex)
            .Take(maximumRows);

UPDATE

You can define the ProcessId as int?[] or more better as IEnumerable<int?>

IEnumerable<int?> ProcessId = biz.Context.tblRoleProcess
                                 ...
                               .Select(x => (int?)x.ProcessId);

or change where as:

.Where(x => ProcessId.Contains(x.ProcessID)
               || x.ParentProcessID.HasValue
               ? ProcessId.Contains(x.ParentProcessID)
               : false);
Sign up to request clarification or add additional context in comments.

7 Comments

@asieh What you mean? What type of GetQuery returns?
this return an IQueryable<tblProcess> , i try your answer but i have compile error:"Instance argument: cannot convert from 'int[]' to 'System.Linq.ParallelQuery<int?>' "
@asieh I mean how defined the tblProcess?
@asieh Try define the ProcessId as int?[].
that is a tree. each process maybe has a parent then "ParentProcessID" is not null. if one process hasn't parent ParentProcessID is null
|
0

Contains is not a method defined above array, but above IEnumerable<T>. It has two overloads and is well documented on MSDN.

First overload (the one you are using) accepts Integer and uses default comparer, that means, two equal integers are

The other overload is with adding a IEqualityComparer instance, which is not really useful in this case.

The most optimal way to use here is not via contains, but via Any - you may use Contains as suggested in other answers, but that will be less effective:

return GetQuery(filterExpression, Columns)
   .Where(x => ProcessId.Any(processIdItem => processIdItem == x.ProcessID || processIdItem == x.ParentProcessId ))
   .OrderBy<Models.tblProcess>(sortExpression)
   .Skip(startRowIndex)
   .Take(maximumRows);

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.