我的天蓝色模型中有三个模型。一个模型基本上映射了另外两个。这是我的模特
public class Organization : EntityModel
{
public String Id
{
get
{
return this.RowKey;
}
set
{
this.RowKey = value;
}
}
......
}
public class OrganizationGroup : EntityModel
{
public String Id
{
get
{
return this.RowKey;
}
set
{
this.RowKey = value;
}
}
.......
}
现在这里是映射类
class OrganizationGroupMapping : EntityModel
{
public String OrganizationId
{
get
{
return this.PartitionKey;
}
set
{
this.PartitionKey = value;
}
}
public String OrganizationGroupId
{
get
{
return this.RowKey;
}
set
{
this.RowKey = value;
}
}
....
}
现在我正在尝试创建一个功能,它将为我提供特定组织组中的所有组织。我可以使用for循环,我现在正在做这个
public IEnumerable<Organization> GetOrganizations()
{
List<Organization> org = new List<Organization>();
foreach (OrganizationGroupMapping ogm in OrganizationGroupMapping.GetByOrganizationGroupId(Id))
{
org.Add(ogm.GetOrganization());
}
return org;
}
从OrganizationGroup类调用上述函数。我正在尝试使用linq,因为它看起来更清洁。但不知道该怎么做。在这方面有任何帮助吗?
答案 0 :(得分:2)
您只需要使用 System.Linq.Select ,这将创建隐式类型为GetOrganization()
的新对象。
public IEnumerable<Organization> GetOrganizations()
{
var organizations = OrganizationGroupMapping.GetByOrganizationGroupId(Id)
.Select(org => ogm.GetOrganization());
return organizations;
}
答案 1 :(得分:0)
您应该学习Navigation Properties和Fluent API
public class Organization
{
public String Id { get; set; }
public virtual ICollection<OrganizationGroup> OrganizationGroups { get; set; }
}
public class OrganizationGroup
{
public String Id { get; set; }
public virtual ICollection<Organization> Organizations { get; set; }
}
在您的背景下:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Organization>().HasMany(l => l.OrganizationGroups).WithMany(t => t.Organizations).Map(m =>
{
m.MapLeftKey("OrganizationId");
m.MapRightKey("OrganizationGroupId");
m.ToTable("OrganizationGroupMapping ");
});
}
用法:
Context.Organizations.Include("OrganizationGroups").ToArray();
答案 2 :(得分:0)
在您的代码中,我看不到您如何在OrganizationGroup和Organization实体上定义分区键。
我了解组织属于组织团体?如果它是正确的,你可以建立你的实体:
因为处理Azure存储表我们有:
要拥有唯一的OrganizationGroup,Id会映射分区键,但我们让rowKey为空。
class ActorA extends Actor {
override def receive: Receive = {
case msg: String => sender ! msg
case msg: Int => sender ! msg.toString
case _ => "Invalid command!"
}
由于组织属于organizationGroup,因此Organization实体的PartitionKey是organizationGroup的分区键(请参阅Azure Storage Table Design Guide,查看“一对多关系”部分):
public class OrganizationGroup : TableEntity
{
public OrganizationGroup()
{
this.RowKey = string.Empty;
}
public String Id
{
get { return this.PartitionKey; }
set { this.PartitionKey = value; }
}
}
因此您不再需要映射表:您可以直接通过OrganizationGroupId(= PartitionKey)检索您的组织:
public class Organization : TableEntity
{
public String OrganizationGroupId
{
get { return this.PartitionKey; }
set { this.PartitionKey = value; }
}
public String Id
{
get { return this.RowKey; }
set { this.RowKey = value; }
}
}