Home > front end >  where to put orderby in linq statement
where to put orderby in linq statement

Time:02-05

Here is my code and i want to order by via date_added column. i tried all the possibilities but still the date_added column sorted via month instead of a year. Please guide where i need to put orderby statement.

  {
            var records = (from r in db2.documents
                           select new
                           {
                               r.show_in_portal,
                               r.buyer_id,
                               r.advertiser_id,
                               r.contract_id,
                               r.campaign_id,
                               date_added = Dates.FormatDateToExt(r.date_added),
                               id = r.document_id,
                               name = r.filename,
                               location = r.filename,
                               r.publisher_id,
                               affiliate_id = (r.contract != null ? r.contract.publisher_id : -1),
                               document_type = r.document_type.type_name
                           });
            if (campaign_id > 0)
                records = records.Where(v => v.campaign_id == campaign_id);
            //if (creativeid > 0)
            //  records = records.Where(v => v.id == creativeid);
            if (affid > 0)
                records = records.Where(v => v.publisher_id == affid);
            if (contid > 0)
                records = records.Where(v => v.contract_id == contid);
            if (advertiserid > 0)
                records = records.Where(v => v.advertiser_id == advertiserid);
            if (buyerid > 0)
                records = records.Where(v => v.buyer_id == buyerid);

            GridOut(context, records.ToArray());
        }

CodePudding user response:

The result of your query is a sequence of some anonymous type. Date_Added is one of the properties of this anonymous type, so after you created your query you can order by Date_Added.

The type of Date_Added is the returned type of Dates.FormatDateToExt(...). Alas you forgot to inform us about this type. Is it a DateTime? Is it a string? If you order by this type do you get the sorting order that you want?

If so, just add the OrderBy at the end:

var records = db2.documents.Select(document => new
{
    Id = document.document_id,

    Portal = document.Show,
    BuyerId = document.buyer_id,
    AdvertiserId = document.advertiser_id,
    ...
    DateAdded = Dates.FormatDateToExt(document.date_added),
});

if (campaign_id > 0)
    records = records.Where(record => record.campaign_id == campaign_id);
if (affid > 0)
    records = records.Where(record => record.publisher_id == affid);
... // etc. other ifs and other wheres

records = records.OrderBy(record => record.DateAdded);

It is a good thing to do the Sorting at the end, because this means that you will have to sort fewer records. All records that don't pass all Wheres, won't have to be sorted.

Finally a small hint: did you see, that if you use proper identifiers, that your queries will be easier to read? It is good practice to use plural nouns for collections of items, and singular nouns for elements of the collection:

var novels = dbContext.Books.Where(book => book.Type == BookType.Novel)

CodePudding user response:

Consider making your dates uniform by using ISO 8601 ones (convert them on the fly in your Linq query), as they're made to be sortable. You can put the orderby clause after the from.

Read:

  •  Tags:  
  • Related