【问题标题】:Help in Entity Framework 4.1 QueryEntity Framework 4.1 查询中的帮助
【发布时间】:2011-06-19 21:57:58
【问题描述】:

我在编写查询时需要帮助,下面我只为这个示例编写了具有必要属性的类(我没有显示 e.f. 生成的 DB 表)

//this class will create a unique id for each location may be country,
 state or city
public  class Location
{
    public int Id { get; set; }
    public string Name { get; set; }       
    public string Discriminator{get;set;} 

    public int? ParentLocationId { get; set; }
    public Location ParentLocation { get; set; }

    public ICollection<Location> ChildLocations { get; set; }
}

位置数据示例

Id | Name | Discriminator | ParentLocationId
1 | India | Country | null
2 | Karnatka | State | 1
3 | Maharashtra | State | 1
4 | Banglore | City | 2


//this will contain all product categories + products itself
public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Category ParentCategory { get; set; }
    public int? ParentCategoryId { get; set; }

    public ICollection<Category> ChildCategories { get; set; }
}

样本类别数据

Id | Name | ParentCategoryId
1  | Electronics | Null
2  | Mobiles | 1
3 | Apple | 2
4 | Nokia | 2
5 | I phone-4 | 3
6 | Nokia-Some Model | 4

我在下面的类中使用了 Type=User 的变量,但我没有在这里显示“用户”类,因为它不包含任何东西 spl

public class Purchase
{
    public int Id { get; set; }

    public User User { get; set; }
    [Required]
    public int UserId { get; set; }

    public Category Category { get; set; }
    [Required]
    public int CategoryId { get; set; }

    public Location Location { get; set; }
    [Required]
    public int LocationId { get; set; }
}

注意购买成功 订单locationId 必须是cityId 并且categoryId 应该是层次结构中最低的,例如categoryId 不能是Mobile,它应该是iphone-4 或 nokia-some-model

采购订单数据示例

Id | CategoryId | LocationId | UserId
1  | 5 | 4  | 1 
1  | 5 | 4  | 2
1  | 5 | 4  | 3

到目前为止,一切对我来说都很好,以下是我的问题

我正在创建过滤机制,其中我提供 2 个东西 locationId(此位置可能是国家、州或城市的 ID)和产品 ID(此产品可能是层次结构中的任何位置,例如,此值可能是电子设备或移动设备的 id 或apple 或 iphone)并获取满足此要求的所有购买清单

例如:我可以制作的示例过滤器

  • a> 查找某个州的所有移动销售(传递移动的 categoryId 和特定状态的 locationId)
  • b> 查找某个国家/地区的所有诺基亚销售(通过诺基亚的 categoryId 和特定国家/地区的 locationId) 等等..

我愿意接受任何建议,如果此问题中有任何未正确解释的内容,您也可以告诉我。

【问题讨论】:

    标签: entity-framework entity-framework-4 entity-framework-4.1 entity-relationship


    【解决方案1】:

    这是层次查询,恕我直言,无法在 linq-to-entities 中有效执行,因为 linq-to-entities 不支持在层次结构中导航所需的递归。

    这些查询在数据库的某些支持下效果最佳 - 如果是 SQL Server 2005 和更新版本,您可以使用CTE and hierarchical queries。 CTE 可以在数据库视图中使用,这些视图又可以映射到 EF 中的实体,但它仍然不允许您创建所需的过滤条件,因为视图只能具有静态结构。

    例如,您可以使用 CTE 在单个记录中定义返回 CityId 及其 CountryId 的视图。然后,您可以使用此视图并将其加入购买并按CountryId 过滤。但是如果您需要通过StateId 进行搜索怎么办?您的视图当前没有定义 StateId 列 - 您可以将其添加为另一列,但这只会使一切复杂化 - 您必须知道是否必须按国家或州过滤。如果你有另一个级别怎么办?还有一个?如果你不能提前说出你有多少级别怎么办?这正是类别可能发生的事情。视图不会帮助你。

    如果 EF 支持表值函数,这可能是可能的 - 您将为位置创建一个表值函数(它将动态返回属于传递的 LocationId 的所有城市)使用 CTE 在内部和用于类别的类似函数和您将在 EDMX 中映射该函数并在 linq-to-entities 查询的连接中使用它们。有一个问题 - EF 还不支持映射表值函数 - 计划用于 next major release

    如果您需要这种搜索,您需要普通的旧 SQL + CTE。

    【讨论】:

    • 我认为你是这个网站上唯一知道 abt 实体框架的人,我看到所有 EF 答案,我看到你了!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2023-03-22
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多