vendredi 5 février 2016

randomize Select do not work

I wrote a code that's going to accidentally bring back row of data

I want to 5 row chosen randomly but do not work and return value

Everything was true, but the part that is related to the random return just do not work anymore

public List<tblInvoice> Admin_GetRowsSendProduct(string StartDate, string EndDate, int status = -1, bool Randomize = false)
{
    LtSProductDataContext db = new LtSProductDataContext(BLLBase.BLLBase.ConnectionString);
    IQueryable<tblInvoice> xxx = db.tblInvoices.Where(p => p.status == true);
    DateTime _StartDate = DateTime.MinValue;
    DateTime _EndDate = DateTime.MinValue;
    if (xConvertor.ToString(StartDate) == "" || xConvertor.ToString(EndDate) == "")
    {
        if (string.IsNullOrEmpty(StartDate) == false)
            _StartDate = BLLBase.xDateTime.DateXorshid2DateMiladi(StartDate.ToString());
        if (string.IsNullOrEmpty(EndDate) == false)
        {
            _EndDate = BLLBase.xDateTime.DateXorshid2DateMiladi(EndDate.ToString());
            if (_StartDate == _EndDate) { _EndDate = _StartDate.AddDays(1); }
        }
        xxx = xxx.Where(p =>
        (p.status == true) &&
                        (_StartDate == DateTime.MinValue || p.Date >= _StartDate) && (_EndDate == DateTime.MinValue || p.Date <= _EndDate));
    }
    else if (xConvertor.ToString(StartDate) != "" && xConvertor.ToString(EndDate) != "")
    {
        _StartDate = BLLBase.xDateTime.DateXorshid2DateMiladi(StartDate.ToString());
        _EndDate = BLLBase.xDateTime.DateXorshid2DateMiladi(EndDate.ToString());

        xxx = xxx.Where(p => p.Date <= _EndDate && p.Date >= _StartDate && p.status == true && p.SendStatus == status);
    }

    xxx = xxx.Where(p => (status == -1 || p.SendStatus == status) && p.status == true).OrderByDescending(p => p.Date);



    if (Randomize)
    {
        Random rnd = new Random();
        xxx = xxx.OrderBy(x => rnd.Next());
        //xxx = xxx.OrderBy(o => Guid.NewGuid());
        xxx = xxx.Take(3);
        return xxx.Where(p => (p.isPostalPayment == null || p.isPostalPayment == false)).ToList();
    }
    else
    {
        return xxx.Where(p => (p.isPostalPayment == null || p.isPostalPayment == false)).ToList();
    }
    //return xxx.Where(p => p.PaymentType != 4).ToList();
}

do not work this section:

xxx = xxx.OrderBy(x => rnd.Next()); 

or

xxx = xxx.OrderBy(o => Guid.NewGuid());




Aucun commentaire:

Enregistrer un commentaire