Dictionary<int, string> D = new Dictionary<int, string>();
D.Add(0, "Insert");
D.Add(1, "Update");
D.Add(2, "Delete");
using (SerasMacEntity SME = new SerasMacEntity())
{
var SQL = (from p in SME.tbl_admin_islem
let testx = D.Where(x => x.Key == p.islem).Select(x => x.Value).FirstOrDefault()
orderby p.tarih descending
select new
{
p.id,
p.islem,
p.tarih
});
Store1.DataSource = SQL;
Store1.DataBind();
}
它給這個錯誤;爲什麼我不能使用字典實體框架
'System.Collections.Generic.KeyValuePair`2' 和只有原始類型 ( '如的Int32,字符串,和GUID')
我使用的LINQ這個方法,但我不能t使用實體。
public class table
{
public int ID { get; set; }
public string Adi { get; set; }
public int IslemID { get; set; }
public table() { }
public table(int _ID, string _Adi, int _IslemID)
{
_ID = ID;
_Adi = Adi;
_IslemID = IslemID;
}
}
public List<table> table_list = new List<table>(new table[]
{
new table{ID=1,Adi="A1",IslemID=0},
new table{ID=2,Adi="A2",IslemID=1},
new table{ID=3,Adi="A3",IslemID=2},
new table{ID=4,Adi="A4",IslemID=1},
new table{ID=5,Adi="A5",IslemID=0},
new table{ID=6,Adi="A6",IslemID=2},
new table{ID=7,Adi="A7",IslemID=0},
new table{ID=8,Adi="A8",IslemID=1},
new table{ID=9,Adi="A9",IslemID=3}
});
public Dictionary<int, string> option_dictionary = new Dictionary<int,string>()
{
{0, "OK"},
{1, "NO"},
{2, "YA"},
{3, "OH"}
};
public void TestL()
{
string b = option_dictionary.Where(x => x.Key == 1).Select(x =>x.Value).First();
var SQL = (from p in table_list
let test = option_dictionary.Where(x => x.Key == p.IslemID).Select(x => x.Value).First()
select new
{
p.ID,
p.Adi,
test
}
);
}
此方法在Linq中工作。它不在實體中工作。
感謝您的幫助。
第一種方法是好的。第二,我不確定。我相信它會將完整的實體加載到內存中,然後將內存中的排序和投影應用到匿名類型中。我認爲,第二種方法效率低於第一種。 – Slauma 2012-01-08 12:49:54