【问题标题】:randomize Select do not work随机选择不起作用
【发布时间】: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());

【问题讨论】:

    标签: c# linq random lambda


    【解决方案1】:
    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;
    }
    

    【讨论】:

    • db.tblInvoices.ToList() 毫无意义,您将结果保存在_Result 中,然后将其覆盖在_Result = _Temp.OrderBy 中。您所做的只是让数据库从数据库中读取一个额外的select * from Invoices,而您从未对它做任何事情。只需执行List&lt;tblInvoice&gt; _Result; 或将声明移至List&lt;tblInvoice&gt; _Result = _Temp.OrderBy
    • 另外,这非常低效,当您在xxx 上执行.ToList() 时,您将数据库中的所有行都返回到_Temp,然后您只保留5 行并将其放入_Result .如果Invoices 表中有很多行,您应该预计这段代码会很慢并使用大量 RAM。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多