Search code examples
silverlight-4.0entities

Silverlight 4 query against DomainContext for new entry


I'm working on a silverlight application that allows users to fill out common forms.

On my mainpage.xaml I have 4 buttons that allow a user to create a new form. Ihave the following code on the click event handler

FormsDS t = new FormsDS();
        Store s = new Store();
        var i = (from j in t.Stores
                 where j.StoreManager.Contains(WebContext.Current.User.Name)
                 select j.ID);


        form newxferform = new form
        {
            xfertype = 1,
            createdate = DateTime.Now,
            createdby = WebContext.Current.User.Name,
            assignedto = WebContext.Current.User.Name,
            Store = Int32.Parse(i.ToString()),
            xferfrom = Int32.Parse(i.ToString()),
            appovalstatus = 1,
            ronum = "0",
            hide = 0
        };

        t.forms.Add(newxferform);
        t.SubmitChanges(createop =>
            {
                Uri target = new Uri("/views/TransferForm.xaml?FormID=" + newxferform.id, UriKind.Relative);
                ContentFrame.Navigate(target);
            }, null);            

FormsDS is my datacontext. I would like to take the value i from the query at the top and assign it to the Store and xferfrom values in newxferform. However when I run it I get 'Input string was not in a correct format'. If I don't do the Int32.Parse I get the message 'Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to '?int'. I don't understand this at all since all of the columns in the db are not null. I've tried writing try catches to handle the null values but I can't figure out why the value is coming back as null, the username matches the storemanager column exactly. I should be getting a result.

I've tried several different ways and I can't seem to get it.

I just need to take the ID field from a table called stores where the colum StoreManager equals the currently logged in user and assign it to the store and xferfrom field in a table called forms when I create the form.

What I think is making this harder than i would like is that I don't have a DataContext defined anyhere in the xaml on the page. This is just a nav page. The data is handled in the pages that are loaded in the Navigation Frame.

Any help would be greatly appreciated.

Thanks,

Neil


Solution

  • The problem is that the LINQ query above produces an IEnumerable<int> rather than a single int.

    To solve this, replace i declaration with the following:

    var i =
      t.Stores
        .FirstOrDefault(j => j.StoreManager.Contains(WebContext.Current.User.Name))
        .ID;
    

    Or if you like the LINQ way:

    var i = (from j in t.Stores
               where j.StoreManager.Contains(WebContext.Current.User.Name)
               select j.ID)
            .FirstOrDefault();
    

    (You should replace FirstOrDefault with Single if exactly 1 item is expected or with SingleOrDefault if 0 or 1 items is expected.

    HTH