Siyamamkela
Siyamamkela

Reputation: 67

Linq and ASP.NET MVC: Order data from latest to previous

 internal static IQueryable<TimeReviewDataModel> GetGridDetails(DiscoLlamaEntities context, string actor)   
    {

        return (from t in context.TimeCaptures
                join jc in context.JobCards on t.JobCardID equals jc.ID into jcSub
                from jc in jcSub.DefaultIfEmpty()
                join cu in context.Companies on jc.CustomerID equals cu.ID into cuSub
                from tg in cuSub.DefaultIfEmpty()
                where (t.CreatedBy == actor)
                orderby t.Date descending
                select new TimeReviewDataModel
                {
                    ID = t.ID,
                    CustomerName = tg.Name,
                    Date = t.Date,
                    StartTime = t.StartTime,
                    EndTime = t.EndTime,
                    Description = t.Description,
                    Category = t.Category,
                    JobCardID = t.JobCardID,
                    VsoTask = t.VsoTaskID,
                    IsBillable = (bool)t.Billable
                })
                .OrderBy(e=>e.Date);
    }

Hi, I'm trying to order my data in a grid. At the top I want the data that was entered today as the grid goes down I want the previously added entries. Currently it mixes the entries. I want to order using the Date property.

I'm using Linq and EntityFramework.

My Grid:

enter image description here

Upvotes: 2

Views: 60

Answers (1)

Peter Smith
Peter Smith

Reputation: 5550

Try the following:

return (from t in context.TimeCaptures
            join jc in context.JobCards on t.JobCardID equals jc.ID into jcSub
            from jc in jcSub.DefaultIfEmpty()
            join cu in context.Companies on jc.CustomerID equals cu.ID into cuSub
            from tg in cuSub.DefaultIfEmpty()
            where (t.CreatedBy == actor)
            //orderby t.Date descending
            select new TimeReviewDataModel
            {
                ID = t.ID,
                CustomerName = tg.Name,
                Date = t.Date,
                StartTime = t.StartTime,
                EndTime = t.EndTime,
                Description = t.Description,
                Category = t.Category,
                JobCardID = t.JobCardID,
                VsoTask = t.VsoTaskID,
                IsBillable = (bool)t.Billable
            })
            .OrderByDescending(e=>e.Date).ThenByDescending(e=>eStartTime);

So, here you are ordering your final dataset, not an intermediate one.

Upvotes: 1

Related Questions