Filtering on Projection Question

Posts   
 
    
MarcoP avatar
MarcoP
User
Posts: 270
Joined: 29-Sep-2004
# Posted on: 04-Jun-2010 22:09:21   

I have the following code that filters using the entity fields but i also want to pass in search criteria and add it on the fly. But since Im using projection, i cant add the filters using the entity fields. does this make sense? basically, how can i write this by adding an additional search criteria??

        public DealAggregateDTO GetMostRecentAggregateByCitySlug(string slug, string search)
        {
            return (from d in MetaData.Deal
                    where d.City.SlugUrl == slug
                    where d.StartDate >= SystemTime.Now()
                    orderby d.StartDate ascending
                    select new DealAggregateDTO
                        {
                            DealId = d.DealId,
                            Description = d.Description
                        });

// add filter here...

        }
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 05-Jun-2010 04:46:34   

So you want to fetch projecting to your DTO and then you want to filter such DTO? Is this what you want?:

public DealAggregateDTO GetMostRecentAggregateByCitySlug(string slug, string search)
        {
            var result = (from d in MetaData.Deal
                    where d.City.SlugUrl == slug
                    where d.StartDate >= SystemTime.Now()
                    orderby d.StartDate ascending
                    select new DealAggregateDTO
                        {
                            DealId = d.DealId,
                            Description = d.Description
                        }).ToList();

            var filteredResult = result.Where(d => d.Description == "something...");
            return filteredResult;

        }
David Elizondo | LLBLGen Support Team
MarcoP avatar
MarcoP
User
Posts: 270
Joined: 29-Sep-2004
# Posted on: 05-Jun-2010 18:56:23   

daelmo wrote:

So you want to fetch projecting to your DTO and then you want to filter such DTO? Is this what you want?:

public DealAggregateDTO GetMostRecentAggregateByCitySlug(string slug, string search)
        {
            var result = (from d in MetaData.Deal
                    where d.City.SlugUrl == slug
                    where d.StartDate >= SystemTime.Now()
                    orderby d.StartDate ascending
                    select new DealAggregateDTO
                        {
                            DealId = d.DealId,
                            Description = d.Description
                        }).ToList();

            var filteredResult = result.Where(d => d.Description == "something...");
            return filteredResult;

        }

Well i dont want to filter in-meory (NO ToList), but do you see how in the first two where statements StartDate belong to the entity, then when you filter by description that belongs to the dto? seems a little weird.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Jun-2010 23:28:38   

It was an example, if you want to filter the result of a projection you must use the fields/propeties of such projection. In my snippet I can't filter on StartDate because it doesn't belong to the class in th result. Please elaborate more to understand what you need and how we can help.

David Elizondo | LLBLGen Support Team