Search code examples
linqsharepointweb-parts

Getting strings from LINQ queries in Sharepoint web part?


I'm setting up a Sharepoint Web Part for tracking language translations for our various products. I can create the translations, now I'm trying to re-populate the form controls if someone chooses an item that that already has a translation, ie they select "French-ProductX" and the various texboxes populate with the data for that translation/product.

My LINQ query is pulling the right data (itemname is the product the user has chosen in a dropdown menu).

   var query = from translation in Translations
               where (Convert.ToString(translation.Name)) == itemname
               select translation;

I can bind the query to a grid and see i'm getting the correct data, but I'm stuck after that. There are two fields (to start with) i want to pull, "Description" and "Features" as strings and fill their corresponding asp:TextBoxes.

I tried converting to array, and it still binds correctly, but it isn't indexing the fields, its viewed aa a single item of type Translation.

I tried an IEnumerable query and converting to a dataview but I got casting errors on the WHERE clause I couldn't resolve.

I tried this, but VS says 'myitem" is an unassigned local variable.

   var query = from translation in Translations
               where (Convert.ToString(translation.Name)) == itemname
               select new { myitem = translation.Description, features = translation.Features };

   ItemDescription.Text = myitem;

This seems like it should be a simple thing, am I just missing something here?


Solution

  • What's happening is you are getting an IEnumerable of an anonymous type with the properties of myitem and features.

    You would need to change the last line to:

    ItemDescription.Text = query.First().myitem;