问题
I am 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 it 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 to do it. Clearly, this is a simple problem, but its confusing me greatly. cboStocks.SelectedItem is an object, and that object is of the anonymous type created by Linq, but thats all I can figure out.
回答1:
Anonymous types are only really useful (and should only be used) with a method. Here you're creating the type in one method when you initialise the combo box and then try and access it in another when reading the selected item. This isn't going to work.
You need to create an actual type to assign to the combo box's ItemsSource
.
回答2:
Unfortunately, there's no good way to do that without reflection. Anonymous types aren't really meant to be stashed and retrieved from later in absence of some big reflection framework to check them out. They're pretty much just designed for temporary convenience in methods that are rearranging data internally.
I suggest that you make a named type with the same three fields; then it's a trivial matter to cast it and get what you want back out.
回答3:
Found the following approach on this blog a while ago, try the following:
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
回答4:
I agree with ChrisF. You should use a concrete type here. However this workaround works if you want to try it out:
T Cast<T>(object obj, T type)
{
return (T)obj;
}
...
var myItem = Cast(cboStocks.SelectedItem, new { st.StockID = 0, su.StockUserID = 0, st.Ticker = "" });
...
回答5:
So here's what I ended up doing, 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";
来源:https://stackoverflow.com/questions/1627360/how-do-i-get-values-from-selecteditem-in-combobox-with-linq-and-c-sharp-3-5