LINQ to Entites 和 ObjectQuery

发布于 2024-09-06 12:50:32 字数 721 浏览 5 评论 0原文

我有一个与 Person_Addresses 具有 1 : N 关系的 Person 实体(字段:PersonIDAddressIDValidFrom) 。我想获取所有 Person 记录以及关联的 Person_Addresses 以及最新的 ValidFrom。我应该如何使用 ObjectQuery 或 IQueryable 来执行此操作?

编辑: 我提到了 ObjectQueryIQueryable,因为我想有一个使用扩展方法的解决方案(我认为,这就是它的调用方式)。另外我忘了提及我正在使用 Entity Framework 生成实体。我想获得一个 person 对象,它的 person_adress 成员已急切地加载。

以下是实体结构: 个人成员:int id、字符串名字、字符串姓氏、Partner_Address 伙伴地址

Person_Address 成员:int personidint adressiddate validfrom

I have Person entity which has a 1 : N relationship with Person_Addresses (fields: PersonID, AddressID, ValidFrom). I want to get all Person records and associated Person_Addresses with only latest ValidFrom. How should I do this using ObjectQuery or IQueryable?

Edit:
I mentioned ObjectQuery and IQueryable, because I wanted to have a solution using extension methods (I think, that how it's called). Also I forgot to mention that I'm using Entity Framework where I have the entities generated. I want to get a person object which has it's person_adress member eagerly loaded.

Here are the entities structure:
Person members: int id, string firstname, string lastname, Partner_Address partneradress

Person_Address members: int personid, int adressid, date validfrom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-09-13 12:50:32

尝试以下操作。

我有以下实体。

    //Person Entity
    public class Person
    {
        public int PersonID  { get; set; }
        public string PersonName { get; set; }
    }

    //PersonAddress Entity
    public class PersonAddress
    {
        public int PersonID { get; set; }
        public int AddressID { get; set; }
        public DateTime ValidFrom { get; set; }
    }

然后触发以下查询。

    //Get the latest ValidFrom for each person from PersonAddress.
    var getLatestDateRecords =
    from p in lstPersonAddress
    group p by p.PersonID into g
    select new
    {
        Infos =
            (from PA in g
            select new
            {
                PersonId = PA.PersonID
                Date = g.Max(t=>t.ValidFrom)
            }).Distinct()
    };

    //Segregate the ValidFroms and PersonId from the
    //previous record set(getLatestDateRecords).
    var segRecords =
        from x in getLatestDateRecords
        from y in x.Infos
        select new { Date = y.Date, PersonId = y.PersonId };

    //Obtain all the relevant information from the PersonAddress
    // for the latest ValidFrom dates.
    var allValidRecords =
        from PA in lstPersonAddress
        join x in segRecords
        on PA.ValidFrom equals x.Date
        where PA.PersonID == x.PersonId
        select new {
                PersonId = PA.PersonID
                , AddressId = PA.AddressID
                , Date = PA.ValidFrom
            };

    //Get the final result
    var resultSet =
        from p in lstPerson
        join x in allValidRecords
        on p.PersonID equals x.PersonId
        select new
        {
            PersonId = p.PersonID
            ,PersonName = p.PersonName
            ,AddressId = x.AddressId,
            Date = x.Date
        };

我发现它在一些测试数据上运行良好。

如有任何疑问,请告诉我。

Try the following.

I have the following entities.

    //Person Entity
    public class Person
    {
        public int PersonID  { get; set; }
        public string PersonName { get; set; }
    }

    //PersonAddress Entity
    public class PersonAddress
    {
        public int PersonID { get; set; }
        public int AddressID { get; set; }
        public DateTime ValidFrom { get; set; }
    }

Then fire the following query.

    //Get the latest ValidFrom for each person from PersonAddress.
    var getLatestDateRecords =
    from p in lstPersonAddress
    group p by p.PersonID into g
    select new
    {
        Infos =
            (from PA in g
            select new
            {
                PersonId = PA.PersonID
                Date = g.Max(t=>t.ValidFrom)
            }).Distinct()
    };

    //Segregate the ValidFroms and PersonId from the
    //previous record set(getLatestDateRecords).
    var segRecords =
        from x in getLatestDateRecords
        from y in x.Infos
        select new { Date = y.Date, PersonId = y.PersonId };

    //Obtain all the relevant information from the PersonAddress
    // for the latest ValidFrom dates.
    var allValidRecords =
        from PA in lstPersonAddress
        join x in segRecords
        on PA.ValidFrom equals x.Date
        where PA.PersonID == x.PersonId
        select new {
                PersonId = PA.PersonID
                , AddressId = PA.AddressID
                , Date = PA.ValidFrom
            };

    //Get the final result
    var resultSet =
        from p in lstPerson
        join x in allValidRecords
        on p.PersonID equals x.PersonId
        select new
        {
            PersonId = p.PersonID
            ,PersonName = p.PersonName
            ,AddressId = x.AddressId,
            Date = x.Date
        };

I found it to be working fine with some test data.

Let me know in case of any concern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文