views:

60

answers:

1

Hi

I have Person enity 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 EF 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

A: 

Try this

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 below 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.

priyanka.sarkar_2
Well I think that you got the idea as how I did that. I don't think that it will be a problem for you to suit your requirement. You can take a look into the Entites upon which I have fired the Linq. It looks very similar that of yours. Hope you can proceed further without much hassel. All the best
priyanka.sarkar_2
Hi, this is a tested program. You can even go ahead with some sample. And more over, it has been done into pieces for the sake of understanding.
priyanka.sarkar_2
I happened to come accros this thread: http://stackoverflow.com/questions/584820/how-do-you-perform-a-left-outer-join-using-linq-extension-methods/584836#584836I wonder if it's possible to join 2 different querys using groupjoin? something like: PersonQuery.GroupJoin(... PersonAddressQuery ...)
Sys