嵌套的 Linq 查询 Microsoft Azure

Nested Linq queries Microsoft Azure

我的天蓝色模型中有三个模型。一个模型基本上映射了另外两个。这是我的模型

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

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 class 调用上述函数。我正在尝试使用 linq,因为它看起来更干净。但不确定该怎么做。在这方面有什么帮助吗?

您只需要使用 System.Linq.Select 即可创建隐含类型为 GetOrganization().

的新对象
public IEnumerable<Organization> GetOrganizations()
{
    var organizations = OrganizationGroupMapping.GetByOrganizationGroupId(Id)
                        .Select(org => ogm.GetOrganization());
    return organizations;
}

你应该研究一下Navigation Properties And 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();

在您的代码中,我看不到您如何在 OrganizationGroup 和 Organization 实体上定义分区键。

我了解一个组织属于一个组织组?如果它是正确的,你可以像那样构建你的实体:

因为处理 Azure 存储 table 我们有:

  • PartitionKey + rowKey = 一行的唯一性

为了拥有唯一的 OrganizationGroup ,Id 映射分区键,但我们让 rowKey 为空。

public class OrganizationGroup : TableEntity
{
    public OrganizationGroup()
    {
        this.RowKey = string.Empty;
    }
    public String Id
    {
        get { return this.PartitionKey; }
        set { this.PartitionKey = value; }
    }
}

因为一个组织属于一个organizationGroup,所以Organization实体的PartitionKey就是organizationGroup的分区键(参见Azure Storage Table Design Guide,看一对多关系部分):

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; }
    }
}

因此您不再需要映射 table:您可以直接通过 OrganizationGroupId(= PartitionKey)检索您的组织:

public static List<Organization> GetByOrganizationGroupId(string organizationGroupId)
{
    var org = new List<Organization>();
    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

    // Create the CloudTable object that represents the "Organization" table.
    CloudTable organizationTable = tableClient.GetTableReference("Organization");

    // Construct the query operation for all OrganizationGroupMapping entities where PartitionKey="organizationGroupId".
    var query = new TableQuery<Organization>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, organizationGroupId));

    // Return the organizations
    return organizationTable.ExecuteQuery(query).ToList();
}