2012-04-14 28 views
9

Làm cách nào để giải quyết vấn đề này?LINQ to Entities không nhận ra phương thức 'System.String get_Item (System.String)',

Đây là mã của tôi:

DateTime dtInicio = new DateTime(); 
    DateTime dtFim = new DateTime(); 
    Int32 codStatus = 0; 

    if(!string.IsNullOrEmpty(collection["txtDtInicial"])) 
     dtInicio = Convert.ToDateTime(collection["txtDtInicial"]); 
    if(!string.IsNullOrEmpty(collection["txtDtFinal"])) 
     dtFim = Convert.ToDateTime(collection["txtDtFinal"]); 
    if (!string.IsNullOrEmpty(collection["StatusCliente"])) 
     Convert.ToInt32(collection["StatusCliente"]); 

    var listCLientResult = (from c in db.tbClientes 
          orderby c.id 
          where (c.effdt >= dtInicio || string.IsNullOrEmpty(collection["txtDtInicial"]) && 
           (c.effdt <= dtFim || string.IsNullOrEmpty(collection["txtDtFinal"])) && 
           (c.cod_status_viagem == codStatus || string.IsNullOrEmpty(collection["StatusCliente"]))) 
           select c); 
    return View(listCLientResult); 

Các lỗi tôi nhận được là:

LINQ to Entities không nhận ra phương pháp 'System.String get_Item (System.String)', mà có thể không được chuyển đổi thành một biểu thức của kho lưu trữ.

+0

xin hãy xem câu trả lời này: http://stackoverflow.com/questions/7259567/linq-to-entities-does-not-recognize-the-method Chúc mừng – MUG4N

+0

Có, hãy xem câu hỏi được đề xuất, câu trả lời tại sao bạn gặp lỗi và http://stackoverflow.com/a/5541505/1109444 sẽ cho bạn biết cách tạo truy vấn hoạt động. – Hari

+0

có thể trùng lặp của [LINQ to Entities không nhận ra phương thức 'System.String ToString()' method] (http://stackoverflow.com/questions/4121863/linq-to-entities-does-not-recognize-the- phương pháp-hệ thống-string-tostring-method) –

Trả lời

28

Truy vấn LINQ được thực hiện dựa vào cơ sở dữ liệu được dịch sang SQL trước khi chúng có thể được thực thi; nhưng collection["txtDtInicial"] không thể dịch sang SQL, vì không có cú pháp SQL tương đương, và dù sao cơ sở dữ liệu không có quyền truy cập vào collection. Bạn cần trích xuất collection["txtDtInicial"] thành biến trước tiên và chỉ sử dụng biến này trong truy vấn.

Dưới đây là những gì tôi sẽ làm gì:

DateTime dtInicio = DateTime.MinValue; 
DateTime dtFim = DateTime.MaxValue; 
Int32 codStatus = 0; 

if(!string.IsNullOrEmpty(collection["txtDtInicial"])) 
    dtInicio = Convert.ToDateTime(collection["txtDtInicial"]); 
if(!string.IsNullOrEmpty(collection["txtDtFinal"])) 
    dtFim = Convert.ToDateTime(collection["txtDtFinal"]); 
if (!string.IsNullOrEmpty(collection["StatusCliente"])) 
    codStatus = Convert.ToInt32(collection["StatusCliente"]); 

var listCLientResult = (from c in db.tbClientes 
         orderby c.id 
         where (c.effdt >= dtInicio) && 
          (c.effdt <= dtFim) && 
          (c.cod_status_viagem == codStatus) 
          select c); 
return View(listCLientResult); 

Bằng cách khởi tạo dtIniciodtFim để MINVALUE và MAXVALUE, bạn không cần phải kiểm tra xem chúng được định nghĩa trong truy vấn.

+3

Người ta phải hỏi tại sao LINQ không thông minh hơn điều này? – PeterX

+2

@PeterX, tốt, Linq đã khá thông minh IMO ... –

+0

Đây là vấn đề của tôi –

5

Truy vấn LINQ cuối cùng được chuyển thành truy vấn SQL và LINQ không biết phải làm gì với Phiên ["Tên người dùng"] (có được mục "Tên người dùng").

Một cách phổ biến để workaround này là chỉ để sử dụng một biến địa phương mà bạn sẽ gán Session [ "UserName"] và rằng bạn sẽ sử dụng trong truy vấn LINQ của bạn ...

như

string loggedUserName = Phiên ["LogedUsername"]. ToString();
var userdetail = dc.faculties.Where (a => a.F_UserName.Equals (loggedUserName)). FirstOrDefault();

tham khảo http://mvc4asp.blogspot.in/

Các vấn đề liên quan