实体框架查询 - 无法获取数据

时间:2015-01-17 18:48:02

标签: c# wpf linq entity-framework linq-to-sql

我正在使用WPF应用程序并使用Entity framework 6.0

我编写了以下查询来从数据库中获取数据,但无法获取任何数据:

var customersList = context.Customers.Include(x => x.ReturnedCustomerItems.Select(y => y.ReturnedLotItem)).Where(x => x.IsDeleted != false).ToList();

备注:

1. context: Database Context Object
2. Customers: DbSet for Customer's table
3. ReturnedCustomerItems: List of Customer Items to be returned.
4. ReturnedLotItem: Lot Item corresponding to Returned Customer Item. Each Customer Item will have a corresponding lot item..

以下是我编写的用于检查数据是否退出数据库的SQL查询:

select * from 
transportapp.dbo.customer cust,
transportapp.dbo.ReturnedCustomerItem ret,
transportapp.dbo.ReturnedLotItem item
where 
cust.CustomerId = ret.CustomerId
and ret.BookingItemId = item.BookingItemId

作为输出,我得到了以下数据记录:

enter image description here

以下是Entity Framework生成的表结构:

CREATE TABLE [dbo].[ReturnedCustomerItem] (
    [BookingItemId]   INT             NOT NULL,
    [IsDeleted]       BIT             NOT NULL,
    [CustomerId]      INT             NOT NULL,
    [ReturnCharge]    DECIMAL (18, 2) NOT NULL,
    [DemurrageCharge] DECIMAL (18, 2) NOT NULL,
    [Weight]          DECIMAL (18, 2) NOT NULL,
    [Quantity]        INT             NOT NULL,
    [ReturnDate]      DATETIME        NOT NULL,
    [Status]          NVARCHAR (100)  NULL,
    CONSTRAINT [PK_dbo.ReturnedCustomerItem] PRIMARY KEY CLUSTERED ([BookingItemId] ASC),
    CONSTRAINT [FK_dbo.ReturnedCustomerItem_dbo.Customer_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([CustomerId]) ON DELETE CASCADE,
    CONSTRAINT [FK_dbo.ReturnedCustomerItem_dbo.ReturnedLotItem_BookingItemId] FOREIGN KEY ([BookingItemId]) REFERENCES [dbo].[ReturnedLotItem] ([BookingItemId])
);

CREATE TABLE [dbo].[ReturnedLotItem] (
    [BookingItemId]   INT             NOT NULL,
    [IsDeleted]       BIT             NOT NULL,
    [IsReturned]      BIT             NOT NULL,
    [ReturnCharge]    DECIMAL (18, 2) NOT NULL,
    [DemurrageCharge] DECIMAL (18, 2) NOT NULL,
    [Weight]          DECIMAL (18, 2) NOT NULL,
    [Quantity]        INT             NOT NULL,
    [LotId]           INT             NOT NULL,
    [LotItemId]       INT             NOT NULL,
    CONSTRAINT [PK_dbo.ReturnedLotItem] PRIMARY KEY CLUSTERED ([BookingItemId] ASC),
    CONSTRAINT [FK_dbo.ReturnedLotItem_dbo.BookingItem_BookingItemId] FOREIGN KEY ([BookingItemId]) REFERENCES [dbo].[BookingItem] ([BookingItemId]),
    CONSTRAINT [FK_dbo.ReturnedLotItem_dbo.ReturnedLot_LotId] FOREIGN KEY ([LotId]) REFERENCES [dbo].[ReturnedLot] ([LotId]) ON DELETE CASCADE,
    CONSTRAINT [FK_dbo.ReturnedLotItem_dbo.LotItem_LotItemId] FOREIGN KEY ([LotItemId]) REFERENCES [dbo].[LotItem] ([BookingItemId]) ON DELETE CASCADE
);

我无法在我出错的地方调试LINQ表达式。需要帮助来识别错误。

谢谢!

1 个答案:

答案 0 :(得分:0)

发现我的愚蠢错误......

这是正确的LINQ表达式:

var customersList = context.Customers.Include(x => x.ReturnedCustomerItems.Select(y => y.ReturnedLotItem)).Where(x => x.IsDeleted == false).ToList();