1
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、String、Guidなど')

Linqでこのメソッドを使用していますが、Entityを使用できません。

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で機能しています。エンティティでは機能しません。

ご協力いただきありがとうございます。

4

2 に答える 2

2

LINQ は LINQ と同じではありません。実際の例では、メモリ内のデータ構造、つまりList<table> table_listより一般的にはIEnumerable<T>. の LINQ 拡張メソッドを使用することをLINQ to ObjectsIEnumerable<T>と呼びます。

ObjectSet<T>またはEntity Frameworkに LINQ を適用する場合DbSet<T>は、 の LINQ 拡張メソッドを使用していますIQueryable<T>。この場合、LINQ to Entitiesを使用しています。それらは同じ名前を持ち、LINQ to Objects のように同じ LINQ 式を記述でき、コードは正常にコンパイルされます。

ただし、大きな違いは、LINQ to Entities クエリは (LINQ to Objects のように) メモリ内で実行されず、データベース内で実行するには SQL に変換する必要があることです。

クエリが SQL に変換できない場合、または変換がサポートされていない場合は、実行時例外が発生します。あなたの特定のケースでは、複雑なメモリ内データ構造 (辞書) があり、SQL クエリでは使用できません。

問題を解決するには、クエリを書き直す必要があります。たとえば、次のように、クエリを実行した後に辞書を使用します。

public class Helper
{
    public int id { get; set; }
    public int islem { get; set; }
    public string textx { get; set; }
    // ...
}

using (SerasMacEntity SME = new SerasMacEntity())
{
    var SQL = (from p in SME.tbl_admin_islem
               orderby p.tarih descending
               select new Helper
               {
                  id = p.id,
                  islem = p.islem,
                  //...
               });

    var list = SQL.ToList(); // excutes query in DB, rest happens in memory
    foreach (var item in list)
    {
        item.testx = D.Where(x => x.Key == item.islem)
                      .Select(x => x.Value)
                      .FirstOrDefault();
    }

    Store1.DataSource = list;
    Store1.DataBind();
}
于 2012-01-07T17:08:59.923 に答える
1

私はそれをこの方法で解決します。

var SQL = (from p in SME.tbl_admin_islem
                   orderby p.tarih descending
                   select new
                   {
                       p.id,
                       p.islem,
                       p.tarih
                   }).AsEnumerable().Select(s => new
                                 {
                                     s.id,
                                     s.islem,
                                     s.tarih,
                                     testx = D.Where(x => x.Key == s.islem).Select(x => x.Value).FirstOrDefault()
                                 });

またはこれを試してください

var SQL = (from p in SME.tbl_admin_islem.AsEnumerable()
                   orderby p.tarih descending
                   select p).Select(s => new
                   {
                       s.id,
                       s.islem,
                       s.tarih,
                       testx = D.Where(x => x.Key == s.islem).Select(x => x.Value).FirstOrDefault()
                   });

エンティティAsEnumerable()を変換してから、Lambdaクエリを再度適用しました。

于 2012-01-08T00:45:06.563 に答える