views:

48

answers:

3

This code is returning me a list that I am trying to pass to another function.

_ucItem.lblItemName.Text = itemRow.Field<string>("ITEM_NAME").ToString();
_ucItem.pbxItemImg.Image = itemRow.Field<string>("IMAGE").ToString();
_ucItem.lblItemName.Text = itemRow.Field<string>("FK_ITEM_ID").ToString();

Here I want to replace this set of lines with the result set that I am trying to Pass. Do I have to declare a class for doing this or is there some way to do the same job without defining structure of a class

private void SomeFunction()
{
   var distinctItems = itemListToCreate
            .Select(dr => new { FK_ITEM_ID = dr.Field<long>("FK_ITEM_ID") }).Distinct()
            .Join(_SelectProgram.FilteredItemDescription, outer => outer.FK_ITEM_ID,
            inner => inner.Field<long>("FK_ITEM_ID"),
            (inner, outer) => new 
                              { 
                                  inner.FK_ITEM_ID,
                                  ItemName = outer.Field<string>("ITEM_NAME"),
                                  ItemImage = outer.Field<byte[]>("IMAGE") 
                              }
            );

    CreateItemsDynamically(distinctItems.ToList());
}

private void CreateItemsDynamically<T>(List<T> itemListToCreate)
{
    itemListToCreate.ForEach(itemRow =>
    {                
        ucItem _ucItem = new ucItem();
        //Compute location for this usercontrol              

        _ucItem.lblItemName.Text = itemRow.Field<string>("ITEM_NAME").ToString();
        _ucItem.pbxItemImg.Image = itemRow.Field<string>("IMAGE").ToString();
        _ucItem.lblItemName.Text = itemRow.Field<string>("FK_ITEM_ID").ToString();

        _ucItem.TodayButton_OnClick += new EventHandler(_ucItem_TodayButton_OnClick);
        _ucItem.MultiDateButton_OnClick += new EventHandler(_ucItem_MultiDateButton_OnClick);

        pnlItemCollection.Controls.Add(_ucItem);
        _SelectProgram.ItemCollectionList.Add(_ucItem);
    });
}
+1  A: 

A type is either explicitly named or anonymous. If the earlier then you declare the type - struct or class.

Preet Sangha
+1  A: 

It's not quite clear to me what you're trying to do. There are ways of using anonymous types across methods, but they're somewhat hacky. I blogged about one way a while ago... but I wouldn't suggest using it. Likewise you could use reflection - but again, I wouldn't recommend that either.

It's a much better idea to create a type which represents the values you want... basically a named version of the anonymous type. The code will be easier to understand and maintain in the long run. (I'd like a future version of C# to be able to declare a type with all the features of currently-anonymous types, but with names... but that's not available now.)

Jon Skeet
+1  A: 

They are anonymous, that is the whole point! You can see the type using reflection but if you need accessing it, just make a type - how difficult is that?

Aliostad