【发布时间】: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