Apress pro LINQ Language Integrated Query in C# 2008 phần 10

LINQ to SQL Code Examples Khi chúng ta đã tạo mẫu cho csdl của chúng ta sử dụng trình thiết kế LINQ to SQL, chúng ta có thể dễ dàng viết mã để làm việc trở lại. Bên dưới là một vài ví dụ môt tả dữ liệu thông thường | 504 CHAPTER 16 THE DATACONTEXT The result is that if you have an entity object cached in your DataContext and another context updates a field for that entity object s record in the database and you perform a LINQ query specifying that field in the search criteria so that it matches the new value in the database the record will be included in the results set. However since you already have it cached you get the cached entity object returned with the field not matching your search criteria. It will probably be clearer if I provide a specific example. What I will do is first query for a specific customer that I know will not match the search criteria I will provide for a subsequent query. I will use customer LONEP. The region for customer LONEP is OR so I will search for customers whose region is WA. I will then display those customers whose region is WA. Next I will update the region for customer LONEP to WA using just as if some other context did it externally to my process. At this point LONEP will have a region of OR in my entity object but WA in the database. Next I will perform that very same query again to retrieve all the customers whose region is WA. When you look in the code you will not see the query defined again though. You will merely see me enumerate through the returned sequence of custs. Remember that because of deferred query execution I need only enumerate the results to cause the query to be executed again. Since the region for LONEP is WA in the database that record will be included in the results set. But since that record s entity object is already cached it will be the cached entity object that is returned and that object still has a region of OR. I will then display each returned entity object s region. When customer LONEP is displayed its region will be OR despite the fact that my query specified it wanted customers whose region is WA. Listing 16-3 provides the code to demonstrate this mismatch. Listing 16-3. An Example Demonstrating

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.