0
public List<Workflow> GetMyWorkflows(int[] MyRoles)
        {
            int[] myWorkflowIDs = new int[] { };
            RapidWorkflowDataContext context = new RapidWorkflowDataContext();
                var query = from w in context.WorkflowRoles
                            where MyRoles.Contains((int)w.RoleID)
                            select w.WorkflowID;
                var distinctWorkflows = query.Distinct();
                myWorkflowIDs = distinctWorkflows.toArray();
                return myWorkflowIDs;
        }

In this method I want to retrieve an array of workflows that a user can access. I get the following error : Cannot implicitly convert type 'int?[]' to 'int[]'

4 Answers 4

1

I want to retrieve an array of workflows

But your method must return a List<Workflow> or a List<int>.

So you should skip the array idea. The other issue is between int and int?. You can solve that in the select clause with select w.WorkflowID.Value or select w.WorkflowID ?? 0. Or simply select w for a List<Workflow>.

Also it is a good idea to dispose a context when it becomes unreachable.

    public List<int> GetMyWorkflows(int[] MyRoles)
    {
        using (RapidWorkflowDataContext context = new RapidWorkflowDataContext())
        {
           var query = from w in context.WorkflowRoles
                    where MyRoles.Contains((int)w.RoleID)
                    select w.WorkflowID ?? 0;
                    // select w;  to return a List<WorkFlow>

           var distinctWorkflows = query.Distinct();

           return distinctWorkflows.ToList();   // ToList because we are closing the Context
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I'm going to guess that WorkflowID is of type int?. If you are certain that it cannot be null, change your central query to:

var query = from w in context.WorkflowRoles
                        where MyRoles.Contains((int)w.RoleID)
                        select w.WorkflowID.Value;

This will ensure that query is now of type IEnumerable<int> instead of IEnumerable<int?>, with the int following on throuhh the Distinct() and ToArray() functions.

3 Comments

var query2 = from w in context.Workflows where myWorkflowIDs.Contains((int)w.ID) select w; List<Workflow> x = query2.FirstOrDefault();
I type that query to get Workflows form Workflow class but it gives me an error: Error Cannot implicitly convert type 'RapidWorkflowRepository.Workflow' to 'System.Collections.Generic.List<RapidWorkflowRepository.Workflow>
@AnasSalem You're getting FirstOrDefault() - which only returns a single object - and assigning it to a list. Do you want the first one, or all of them (in which case, use query2.ToList())?
0

This seems like a pretty good error to me

Cannot convert type 'int?[]' to 'int[]'

You must have an array of type int? and be trying to implicitly convert it to int.

Therefore you have two options - stop trying to implicitly convert, and allow the result to be int?[], like this:

int?[] myWorkflowIDs = new int?[] { };

or force the convert to take place, like this:

RapidWorkflowDataContext context = new RapidWorkflowDataContext();
var query = from w in context.WorkflowRoles
        where MyRoles.Contains((int)w.RoleID)
        select (int)w.WorkflowID;
        // or w.WorkflowID ?? 0; as necessary

Comments

0

So int? can also be written Nullable<int> which is basically an int that can take null values. For example:

int? nullableNumber = 5;   // Set to a value
nullableNumber = null?     // Set to null (which is possible because int? is nullable).

As you can imagine, Nullable<int> is useful for databases because sometimes you might have a column that has null values, and so this type gives a useful means of mapping to this sort of value. The problem, though is that in your code you have to deal with two different types, int vs. int?. You can cast between the two values by using:

// If the nullable-integer is not-null then use it's value, else default to `0`.
int nonNullable = nullableNumber ?? 0; 

which will replace nulls with 0 if the value is null. Or you can just store your myWorkflowIDs in a nullable value (Nullable<int>[] or int?[]), which semantically better reflects what the column value in the database actually is.

3 Comments

That will throw an exception if there is a null value, not just use a 0.
You can use either GetValueOrDefault or the null coalesce operator to do that much more concisely.
Man. I should be giving you rep, heh. Thanks again!

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.