Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MongoDB-使用.NET驱动程序和LINQ进行多个查找(带分组和筛选)_C#_Mongodb_Mongodb Query_Aggregation Framework_Mongodb .net Driver - Fatal编程技术网

C# MongoDB-使用.NET驱动程序和LINQ进行多个查找(带分组和筛选)

C# MongoDB-使用.NET驱动程序和LINQ进行多个查找(带分组和筛选),c#,mongodb,mongodb-query,aggregation-framework,mongodb-.net-driver,C#,Mongodb,Mongodb Query,Aggregation Framework,Mongodb .net Driver,给出以下示例性MongoDB集合模型: public class House { public int Id { get; set; } public string Address { get; set; } public double SquareFeet { get; set; } public int NumberOfBedrooms { get; set; } public int NumberOfKitchens { get; set; } }

给出以下示例性MongoDB集合模型:

public class House
{
    public int Id { get; set; }
    public string Address { get; set; }
    public double SquareFeet { get; set; }
    public int NumberOfBedrooms { get; set; }
    public int NumberOfKitchens { get; set; }
}

public class Mortgage
{
    public int Id { get; set; }
    public int HouseId {get; set; } // FK 
    public decimal Sum { get; set; }
    public string CurrencyName { get; set; }
}

public class Mortgagee
{
    public int Id { get; set; }
    public int MortgageId { get; set; } // FK
    public string InstitutionName { get; set; }
    public string InstitutionAddress { get; set; }
}

public class Mortgagor
{
    public int Id { get; set; }
    public int MortgageId { get; set; } // FK
    public string Name { get; set; }
    public string Address { get; set; }
}


public class Owner
{
    public int Id { get; set; }
    public int HouseID { get; set; } // FK
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
我希望生成以下查询输出:

{
    "House": {
        "Id": 123,
        "Address": "Some city, 1234, Unknown St.",
        "SquareFeet": "12345.67",
        "NumberOfBedrooms": 4,
        "NumberOfKitchens": 2,
    },
    "Mortgages": [
        {
            "Id": 234,
            "HouseId": 123,
            "Sum": 1234.56,
            "CurrencyName": "USD",
            "Mortgagee": {
                "Id": 345,
                "MortgageId": 234,
                "InstitutionName": "Some institution",
                "InstitutionAddress": "Some city, 5678, Unknown St."
            },
            "Mortgagors": [
                {
                    "Id": 456,
                    "MortgageId": 234,
                    "Name": "John Smith",
                    "Address": "Some city, 1234, Unknown St."
                },
                {
                    "Id": 567,
                    "MortgageId": 234,
                    "Name": "Ann Smith",
                    "Address": "Some city, 1234, Unknown St."
                }
            ]
        }
    ],
    "Owners": [
        {
            "Id": 678,
            "HouseId": 123,
            "FirstName": "John",
            "LastName": "Smith"
        },
        {
            "Id": 789,
            "HouseId": 123,
            "FirstName": "Ann",
            "LastName": "Smith"
        }
    ]
}
我知道可以使用来执行连接操作,但是我能找到的所有示例(在MongoDB文档和博客等中)都非常简单,并且不包括我想要执行的每个查找(聚合)的任何分组和筛选操作,因为实际模型更复杂

因此,基本上我想做的是:

  • 为房屋创建过滤器
  • 匹配匹配房屋过滤器的所有房屋并将其分组
  • 查找房屋的所有抵押贷款
  • 筛选抵押贷款并将其分组
  • 查找所有抵押贷款的所有抵押权人
  • 查找所有抵押贷款的所有抵押人
  • 查找所有房主的房子
  • 筛选所有者并将其分组
  • 这应该在一个DB调用内发生

    我现在能做的是:

    public class HouseResult: House
    {
        public IList<Mortgage> Mortgages { get; set; }
        public IList<Owner> Owners { get; set; }
    }
    
    var housesWithMortgagesAndOwners = housesCollection
        .Aggregate()
        .Match(houseFilter)
        .Lookup<House, Mortgage, HouseResult>(
            mortgagesCollection,
            localField => localField.Id,
            foreignField => foreignField.HouseId,
            output => c.Mortgages)
        .Lookup<HouseResult, Owner, HouseResult>(
            ownersCollection,
            localField => localField.Id,
            foreignField => foreignField.HouseId,
            c => c.Owners)
        .ToList();
    
    公共类房屋结果:房屋
    {
    公共IList抵押{get;set;}
    公共IList所有者{get;set;}
    }
    var housesWithMortgagesAndOwners=房屋集合
    .Aggregate()
    .匹配(室内过滤器)
    .查找(
    抵押贷款催收,
    localField=>localField.Id,
    foreignField=>foreignField.HouseId,
    输出=>c.0)
    .查找(
    所有者收藏,
    localField=>localField.Id,
    foreignField=>foreignField.HouseId,
    c=>c.Owners)
    .ToList();
    
    但是,我不确定如何对嵌套数组执行
    Lookup()
    (在上面的示例中-如何查找每个抵押的所有抵押权人/抵押人。我知道我可以
    Unwind()
    array,但是我以前构建的结构将消失,因为我将用抵押列表替换我的根目录

    此外,我现在确定如何有效地将分组/筛选应用于
    Lookup()


    谢谢你的建议!

    你检查了吗?是的,我检查了,但是我在那里找不到任何有用的信息。我遗漏了什么吗?检查此语法:你检查了吗?是的,我检查了,但是我在那里找不到任何有用的信息。我遗漏了什么吗?检查此语法: