zoukankan      html  css  js  c++  java
  • net 总数据中取随机几条数据

                    List<string> lstSample = new List<string>();              
                    Random rand = new Random();
                    List<int> lstRand = new List<int>();
                    for (int i = 0; lstRand.Count() < sampleNum; i++)
                    {
                        int mRandNum = rand.Next(0, mStageDataCount);
                        if (!lstRand.Contains(mRandNum))
                        {
                            lstRand.Add(mRandNum);
                            lstSample.Add(lstDataAll.ToList()[mRandNum]);
                        }
                    }  
    View Code

     平均等判断

     List<string> lstSampleRangIdSel = new List<string>(); //去掉同一重复的
                    List<string> lstStageBuildId = IProjectBuildHouseStage.Select(m => m.BuildingId).Distinct().ToList();  //分期所有的
                    //余数
                    int mQuotient = sampleNum / lstBuilding.Count();
                    if (mQuotient > 0)
                    {
                        //少了的楼房数据
                        int mLost = 0;
                        foreach (var buildId in lstBuilding)
                        {
                            Random rand = new Random();
                            List<int> lstRand = new List<int>();
                            var lstProjectBuildHouseStageQuotient = IProjectBuildHouseStage.Where(m => m.BuildingId == buildId);
                            int mBuildHouseCount = lstProjectBuildHouseStageQuotient.Count();
                            //余数大于楼房数
                            if (mQuotient > mBuildHouseCount)
                            {
                                if (lstProjectBuildHouseStageQuotient.Any())
                                {
                                    lstProjectBuildHouseSample.AddRange(lstProjectBuildHouseStageQuotient.ToList());
                                }
                                mLost = mLost + mQuotient - mBuildHouseCount;
                                continue;
                            }
    
                            for (int i = 0; lstRand.Count() < mQuotient; i++)
                            {
                                int mRandNum = rand.Next(0, mBuildHouseCount);
                                if (!lstRand.Contains(mRandNum))
                                {
                                    lstRand.Add(mRandNum);
                                    lstProjectBuildHouseSample.Add(lstProjectBuildHouseStageQuotient.ToList()[mRandNum]);
                                }
                            }
                        }
    
                        //模数
                        int mRemainder = sampleNum % lstBuilding.Count() + mLost;
                        if (mRemainder > 0)
                        {
                            Random rand = new Random();
                            List<int> lstRand = new List<int>();
                            for (int i = 0; lstRand.Count() < mRemainder; i++)
                            {
                                int mRandNum = rand.Next(0, mStageDataCount);
                                if (!lstRand.Contains(mRandNum))
                                {
                                    string strIdSel = IProjectBuildHouseStage.ToList()[mRandNum].BuildingId;
                                    var lstExcept = lstStageBuildId.Where(m => m != strIdSel).Except(lstSampleRangIdSel);
                                    if (lstExcept.Any() && lstSampleRangIdSel.Contains(strIdSel))
                                    {
                                        continue;
                                    }
                                    lstRand.Add(mRandNum);
                                    lstProjectBuildHouseSample.Add(IProjectBuildHouseStage.ToList()[mRandNum]);
                                    if (!lstSampleRangIdSel.Contains(strIdSel))
                                    {
                                        lstSampleRangIdSel.Add(strIdSel);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Random rand = new Random();
                        List<int> lstRand = new List<int>();
                        for (int i = 0; lstRand.Count() < sampleNum; i++)
                        {
                            int mRandNum = rand.Next(0, mStageDataCount);
                            
                            if (!lstRand.Contains(mRandNum) )
                            {
                                string strIdSel = IProjectBuildHouseStage.ToList()[mRandNum].BuildingId;
                                var lstExcept = lstStageBuildId.Where(m => m != strIdSel).Except(lstSampleRangIdSel);
                                if (lstExcept.Any() && lstSampleRangIdSel.Contains(strIdSel))
                                {
                                    continue;
                                }
                                lstRand.Add(mRandNum);
                                lstProjectBuildHouseSample.Add(IProjectBuildHouseStage.ToList()[mRandNum]);
                                if (!lstSampleRangIdSel.Contains(strIdSel))
                                {
                                    lstSampleRangIdSel.Add(strIdSel);
                                }
                            }
                        }
                    }
    View Code

    --作废

    //余数
                    int mQuotient = sampleNum / lstBuilding.Count();
                    if (mQuotient > 0)
                    {
                        //少了的楼房数据
                        int mLost = 0;
                        foreach (var buildId in lstBuilding)
                        {
                            Random rand = new Random();
                            List<int> lstRand = new List<int>();
                            var lstProjectBuildHouseStageQuotient = IProjectBuildHouseStage.Where(m => m.BuildingId == buildId);
                            int mBuildHouseCount = lstProjectBuildHouseStageQuotient.Count();
                            //余数大于楼房数
                            if (mQuotient > mBuildHouseCount)
                            {
                                if (lstProjectBuildHouseStageQuotient.Any())
                                {
                                    lstProjectBuildHouseSample.AddRange(lstProjectBuildHouseStageQuotient.ToList());
                                }
                                mLost = mLost + mQuotient - mBuildHouseCount;
                                continue;
                            }
    
                            for (int i = 0; lstRand.Count() < mQuotient; i++)
                            {
                                int mRandNum = rand.Next(0, mBuildHouseCount);
                                if (!lstRand.Contains(mRandNum))
                                {
                                    lstRand.Add(mRandNum);
                                    lstProjectBuildHouseSample.Add(lstProjectBuildHouseStageQuotient.ToList()[mRandNum]);
                                }
                            }
                        }
    
                        //模数
                        int mRemainder = sampleNum % lstBuilding.Count() + mLost;
                        if (mRemainder > 0)
                        {
                            Random rand = new Random();
                            List<int> lstRand = new List<int>();
                            for (int i = 0; lstRand.Count() < mRemainder; i++)
                            {
                                int mRandNum = rand.Next(0, mStageDataCount);
                                if (!lstRand.Contains(mRandNum))
                                {
                                    lstRand.Add(mRandNum);
                                    lstProjectBuildHouseSample.Add(IProjectBuildHouseStage.ToList()[mRandNum]);
                                }
                            }
                        }
                    }
                    else
                    {
                        Random rand = new Random();
                        List<int> lstRand = new List<int>();
                        for (int i = 0; lstRand.Count() < sampleNum; i++)
                        {
                            int mRandNum = rand.Next(0, mStageDataCount);
                            if (!lstRand.Contains(mRandNum))
                            {
                                lstRand.Add(mRandNum);
                                lstProjectBuildHouseSample.Add(IProjectBuildHouseStage.ToList()[mRandNum]);
                            }
                        }
                    }
    View Code
  • 相关阅读:
    Linux下vi命令大全
    Ubuntu的cron日志在哪里?
    如何使用DNN中的Calendar控件
    对DNN的理解:
    “SQL Server does not allow remote connections”错误的解决
    如何去除Search Skin ojbect中的"web"和"site"选项按键
    DNN发邮件通知4.8.2有漏洞,最好升级到新版本
    模块开发中一点疑惑?
    经典ASP代码大集合
    漂亮button
  • 原文地址:https://www.cnblogs.com/love201314/p/8489398.html
Copyright © 2011-2022 走看看