c# - 在 Entity Framework 中跨多个表进行选择,从而产生通用的 IQueryable?

标签 c# linq entity-framework generics

我有一种情况,我创建了一个方法,该方法接受需求(规范)列表。

这些规范是在我的 PriceSpecification 对象中指定的,它有一个可以为 null 的属性列表,可以按要求工作(各种枚举或 ID)。

我使用 Entity Framework,我的规范可以是来自三个不同表的属性。

我想使用 LINQ 创建一个临时对象,如代码所示。然后根据规范中指定的属性,我想找到正确的对象并使用工厂模式制作 C# 对象。

当我使用单个表时,通常我会执行以下操作:

public List<Customer> GetCustomerBySpecification(CustomerSpecification specification)
    {
        IQueryable<DbCustomers> dbCustomers = repository.DbCustomers;

        if (specification.Id > 0)
        {
            dbCustomers = dbCustomers.Where(c => c.Id == specification.Id);
        }
        if (!string.IsNullOrEmpty(specification.Email))
        {
            dbCustomers = dbCustomers.Where(c => c.Email == specification.Email);
        }
        if (!string.IsNullOrEmpty(specification.ResetPasswordKey))
        {
            dbCustomers = dbCustomers.Where(c => c.ResetPasswordKey == specification.ResetPasswordKey);
        }

        return customerFactory.Create(dbCustomers.OrderBy(c=>c.Id).Skip(specification.Skip).Take(specification.Take).ToList());
    }

但是,现在我使用三个表。我试图制作您可以在下面看到的代码。问题是 where 语句给我错误:Cannot apply operator '==' to operands of ype 'int' and 'LetterColor', candidates are: [...]

从 test_obj.Where() 中,我只需要能够从连接在一起的三个表中获取 ID 属性。

 public Price GetPriceBySpecification(PriceSpecification specification)
        {
            var test_obj = from d in repository.DbPricing
                join d1 in repository.DbOfficeProducts on d.OfficeProductId equals d1.Id
                join d2 in repository.DbOfficeProductDetails on d1.ProductDetailsId equals d2.Id
                select new
                {
                    PricingId = d.Id,
                    LetterColor = d2.LetterColor,
                    LetterPaperWeight = d2.LetterPaperWeight
                };


            if (specification.LetterColor.HasValue)
            {
                test_obj = test_obj.Where(c => c.LetterColor == specification.LetterColor.Value);
            }
            if (specification.LetterPaperWeight.HasValue)
            {
                test_obj = test_obj.Where(c => c.LetterPaperWeight == specification.LetterPaperWeight.Value);
            }

            // Convert found data into a C# object using a Factory class

            // Return awesome stuff

            throw new NotImplementedException();
        }

所以我的问题是:

如何修改我的 GetPriceBySpecification(PriceSpecification specification) 方法,以便我可以使用具有所有三个表的属性的 where 语句,然后获取 ID? :)

编辑:

添加了更多代码:

public class PriceSpecification:Specifications
    {
        public LetterColor? LetterColor { get; set; }
        public LetterPaperWeight? LetterPaperWeight { get; set; }
        public LetterProcessing? LetterProcessing { get; set; }
        public LetterSize? LetterSize { get; set; }
        public LetterType? LetterType { get; set; }
    }

这些类型是这样的类型:

public enum LetterColor
{
    BlackWhite=0,
    Color=1
}

最佳答案

 public Price GetPriceBySpecification(PriceSpecification specification)
    {
        var test_obj = from d in repository.DbPricing
            join d1 in repository.DbOfficeProducts on d.OfficeProductId equals d1.Id
            join d2 in repository.DbOfficeProductDetails on d1.ProductDetailsId equals d2.Id
            //select new <- anonymous object 
            select new Price
            {
                PricingId = d.Id,
                LetterColor = d2.LetterColor,
                LetterPaperWeight = d2.LetterPaperWeight
            };


        if (specification.LetterColor.HasValue)
        {
            test_obj = test_obj.Where(c => c.LetterColor == specification.LetterColor.Value);
        }
        if (specification.LetterPaperWeight.HasValue)
        {
            test_obj = test_obj.Where(c => c.LetterPaperWeight == specification.LetterPaperWeight.Value);
        }

        // Convert found data into a C# object using a Factory class

        // Return awesome stuff

        throw new NotImplementedException();
    }

关于c# - 在 Entity Framework 中跨多个表进行选择,从而产生通用的 IQueryable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21799455/

相关文章:

c# - 如何让我的 C# PictureBox 传播鼠标事件? (提供截图)

c# - 将列表转换为列表<string>

c# - 如何防止 NServiceBus 回滚事务或事务的一部分?

asp.net-mvc - AspNetUserRoles 表未使用 MVC 5 中的 Entity Framework 数据库优先填充

c# - 为什么我无法使用 ef6 code First mysql 迁移来更新数据库?

c# - 在 MVC6 中,如何阻止直接访问 wwwroot 中的文件夹?

c# - 异步与线程,何时使用每个选项?

c# - 如何使用C#在WinRT上使用Google Analytics API

c# - 如何将匿名类型的 List 转换为 List<T>?

linq - 如何在 lambda 表达式中使用 like 运算符