随机选择不起作用

时间:2016-02-06 06:29:36

标签: c# linq random lambda

我编写了一个不小心带回数据行的代码

我想随机选择5行但不起作用并返回值

一切都是真的,但与随机回归相关的部分不再起作用了

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();
}

不适用于此部分:

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

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

1 个答案:

答案 0 :(得分:0)

if (Randomize)
{
    List<tblInvoice> _Result = db.tblInvoices.ToList();

    var _Temp = xxx.Where(p => (p.isPostalPayment == null || p.isPostalPayment == false)).ToList();
    _Result = _Temp.OrderBy(o => Guid.NewGuid()).Take(5).ToList();
    return _Result;
}