简体   繁体   English

具有动态Linq的实体框架

[英]Entity Framework with Dynamic Linq

Using EntityFramework context i need to search with many fields. 使用EntityFramework上下文,我需要搜索许多字段。

The EntityDBContext includes EntityDBContext包括

public class Brand
{
    public int BrandID { get; set; }
    public string BrandName { get; set; }
    public string BrandDesc { get; set; }
    public string BrandUrl { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

public class Product
{
    public string Name {get;set;}
            public DateTime CreatedDate {get;set;}    
            public DateTime ExpiryDate {get;set;}
    //The product class also contains many fields.(not shown here)
}

var context = new EntityDBContext();

I would like to search the brand with using the field in Product. 我想使用“产品”中的字段来搜索品牌。 The fields of the product are only known at run time. 产品的字段仅在运行时已知。 How can i build the expression to search the brand using the product fields. 如何使用产品字段构建表达式以搜索品牌。 Please see the screenshot. 请看截图。 Thanks, Binod 谢谢,Binod

用户搜索表

First off, I'm a bit unclear on this part of your question: 首先,我对您的问题的这一部分不清楚:

the fields of the product are only known at run time.
How so? 怎么会这样? Can you elaborate on this, because I don't see a working implementation of this using EF. 您能否详细说明一下,因为我没有看到使用EF的有效实现。 What is your database table ( Products presumably) set up for? 您的数据库表(可能是Products )设置了什么? What properties are in that class? 该类中有哪些属性?

We need to know that before we can give you an accurate answer . 我们需要知道这一点,然后才能为您提供准确的答案 However, I'll give you a more general example, maybe that helps you in understanding. 但是,我将给您一个更一般的示例,也许可以帮助您理解。

var all_brands_that_sell_shoes = /* But not always ONLY shoes */
        myDataContext.Brands
                     .Where(brand =>
                                 brand.Products.Any(product =>
                                                          product.Type == "Shoe")
                            )
                     .ToList();

Edit 编辑

If I re-read your question, you don't mean that the Product class' properties are not know until runtime; 如果我重新阅读了您的问题,您的意思是说直到运行时才知道Product类的属性; but that you don't know in advance which filters need to be applied and which need to be skipped? 但是您事先不知道需要应用哪些过滤器以及需要跳过哪些过滤器?

This second answer assumes that is what you want. 第二个答案假设这就是您想要的。 Again, I don't know your class' properties since you didn't post them, so I'm inventing my own fields for the sake of example. 同样,由于您没有发布您的类的属性,因此我不知道它们的属性,因此出于示例的目的,我发明了自己的字段。

First, make an object like below. 首先,制作如下所示的对象。 This will be used to aggregate all filters you wish to apply to the selection: 这将用于汇总您希望应用于选择的所有过滤器:

public class MySearchCriteria
{
    public string ProductName_Contains   { get; set; }
    public int    ProductWeight_LessThan { get; set; }
    public int    ProductType_Id         { get; set; }
}

When you want to filter the list, you pass a MySearchCriteria object to the method: 当您要过滤列表时,可以将MySearchCriteria对象传递给该方法:

public List<Brand> GetFilteredList(MySearchCriteria filters)
{
    var brands = myDataContext.Brands; //All brands (for now)

    if(filters.ProductType_Id > 0)
    {
          //IF the filter has a value, filter the list accordingly:
          brands = brands.Where(brand => brand.Products.Any(product => product.TypeId == filters.ProductType_Id);
    }

    if(filters.ProductWeight_LessThan > 0)
    {
          //IF the filter has a value, filter the list accordingly:
          brands = brands.Where(brand => brand.Products.Any(product => product.Weight < filters.ProductWeight_LessThan));
    }

    if(!String.IsNullOrWhiteSpace(filters.ProductName_Contains))
    {
          //IF the filter has a value, filter the list accordingly:
          brands = brands.Where(brand => brand.Products.Any(product => product.Name.Contains(filters.ProductName_Contains)));
    }

     return brands.ToList();
}

This method makes sure that the list was filtered according to the SearchCriteria you provided. 此方法确保根据您提供的SearchCriteria过滤列表。

If you didn't use the field filters.ProductName_Contains for example, it would be an empty string, and the if-evaluation would've prevented you from filtering based on an empty string. 如果您不使用字段过滤器。例如, filters.ProductName_Contains它将是一个空字符串,并且if-evaluation会阻止您基于空字符串进行过滤。 In the end, you would not have applied a name-based filter. 最后,您将不会应用基于名称的过滤器。

I hope this is the answer you were looking for? 我希望这是您想要的答案? If not, please elaborate more as I'm having trouble understanding what it is you want then. 如果没有,请详细说明,因为我当时无法理解您想要的是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM