Salvatore Fanale
Salvatore Fanale

Reputation: 113

Conditional LINQ query gets unassigned variable error?

I get from this question (linq with unassigned variable parameters) that the delayed execution of linq queries explains why I get an unassigned variable error, and in the past this code has worked fine as a work around:

    public static void LoadProjects()
    {
        CHOCoreDataContext dc = new CHOCoreDataContext();
        IEnumerable<ChoCore_Linq_WPF.PROJECT> query1;

        if (((MainWindow)System.Windows.Application.Current.MainWindow).chkShowActive.IsChecked == true)
        {
            query1 =
            from x in dc.PROJECTs
            orderby x.Experiment_Name
            where x.Active == "True" //filter for active projects only
            select x;
        }
        else
        {
            query1 =
            from x in dc.PROJECTs
            orderby x.Experiment_Name
            select x;
        }

        List<ProjectID> items = new List<ProjectID>();
        foreach (var thing in query1)
        {
            items.Add(new ProjectID() { pName = thing.Experiment_Name, pActive = thing.Active, pNotes = thing.Notes }); //creates an "item" with the two properties
        }

        ((MainWindow)System.Windows.Application.Current.MainWindow).lbxProjects.ItemsSource = items; //bind the listbox to the items list
    }

I declare the query before the IF statement and it works just fine. However, in another method I tried to accomplish the same thing using a SWITCH statement:

    public static bool ProjectExists(string myProjectID, modPID _modPID)
    {
        CHOCoreDataContext dc = new CHOCoreDataContext();
        IEnumerable<ChoCore_Linq_WPF.PROJECT> duplicate;

        switch (_modPID)
        {
            case (modPID.addPID):
                duplicate =
                   (from x in dc.PROJECTs
                    where x.Experiment_Name == myProjectID
                    select x);
                break;
            case (modPID.editPID):
                duplicate =
                    (from x in dc.PROJECTs
                     where x.Experiment_Name == myProjectID 
                        && x.PROJECTID_PK != FindProjectKey(myProjectID)
                     select x);
                break;
        }


        if (duplicate.Count() == 0)
        {
            return false;
        }
        else
        {
            return true;
        }

    }  

I am getting an error in the lower IF statement that the variable "duplicate" is unassigned. I understand what that means, but I don't see why it isn't being assigned. I thought maybe the SWITCH statement messes with the scope of the variable so tried an IF statement to construct the linq query, but I get the same error. Do I need to type the query differently, using something other than IEnumerable<>? OR can you suggest what I need to do differently?

Thanks in advance!

Upvotes: 1

Views: 83

Answers (1)

acelent
acelent

Reputation: 8135

You need a default switch case that assigns something to the variable, otherwise it's not definitely assigned before used.

Upvotes: 4

Related Questions