【发布时间】:2016-02-06 06:29:35
【问题描述】:
我写的代码会意外带回一行数据
我想随机选择 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());
【问题讨论】: