How to get values ​​from SelectedItem in ComboBox with Linq and C # 3.5

I'm really missing something with anonymous types because I can't figure out what to do with the Combobox.SelectedItem property.

Here's the code that populates the combobox and works just fine

         var stocks = from st in brdc.tb_dStocks
                     join su in brdc.tb_rStockUsers on st.StockID equals su.StockID
                     where su.UserID == userRec.UserID
                     select new { st.StockID, su.StockUserID, st.Ticker };

        cboStocks.ItemsSource = stocks;
        cboStocks.DisplayMemberPath = "Ticker";

      

Then, when someone selects an item using the cboStocks combobox, I need to figure out what that item is, but I have no idea how. Clearly this is a simple problem, but it really confuses me. cboStocks.SelectedItem is an object and this object is of an anonymous type created by Linq, but that's all I can figure out.

+2


source to share


5 answers


Anonymous types are really useful (and should only be used) with a method. Here, you create a type in one method when you initialize the combo box and then try to access it in another when reading the selected item. It won't work.



You need to create an actual type to assign to the combo box ItemsSource

.

+4


source


Unfortunately, there is no good way to do this without thinking. Anonymous types are not really meant to be hidden and retrieved later, in the absence of some large reflection framework to test them. They are largely meant for temporary convenience in techniques that rebuild data internally.



I suggest that you create a named type with the same three fields; then it's a trivial thing to throw it up and get what you want to fall back on.

+2


source


Found the following approach on this blog a while ago, try this:

private List<T> MakeList<T>(T itemOftype)
{
    List<T> newList = new List<T>();
    return newList;
} 

//create a fake type for anonymous type

var stockType = new {StockID = 0, StockUserId =0, Ticker = string.Empty};

var listOfStocks = MakeList(stockType);

var listOfStocksAnonymous = from st in brdc.tb_dStocks
                 join su in brdc.tb_rStockUsers on st.StockID equals su.StockID
                 where su.UserID == userRec.UserID
                 select new { st.StockID, su.StockUserID, st.Ticker };

listOfStocks = listOfStocksAnonymous.ToList<stockType>();

//now you have a direct access to all anonymous properties  

      

+2


source


I agree with ChrisF. You must use a specific type here. However, this workaround works if you want to try:

T Cast<T>(object obj, T type)
{
    return (T)obj;
}

...
var myItem = Cast(cboStocks.SelectedItem, new { st.StockID = 0, su.StockUserID = 0, st.Ticker = "" });
...

      

0


source


So here's what I did, seems to work pretty well

private class StockInfo
        {
            public int StockID { get; set; }
            public int StockUserID { get; set; }
            public string Ticker { get; set; }

            public StockInfo(int stockID, int stockUserID, string ticker)
            {
                StockID = stockID;
                StockUserID = stockUserID;
                Ticker = ticker;
            }
        }          



BaxRunDataContext brdc = new BaxRunDataContext();
                IEnumerable<StockInfo> stocks = from st in brdc.tb_dStocks
                             join su in brdc.tb_rStockUsers on st.StockID equals su.StockID
                             where su.UserID == userRec.UserID
                             select new StockInfo(st.StockID, su.StockUserID, st.Ticker);

                cboStocks.ItemsSource = stocks;
                cboStocks.DisplayMemberPath = "Ticker";

      

0


source







All Articles