• 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
  • 相关阅读:
    Model I/O
    How to create realistic .scn files?
    3ds Max 教程
    ELK Stack企业日志平台文档
    源码搭建Zabbix4.0.23LTS监控系统
    Ansible自动化运维应用实战
    网站架构部署
    MySQL数据库性能优化与监控实战(阶段四)
    MySQL数据库企业集群项目实战(阶段三)
    基于xtrabackup的主从同步
  • 原文地址:https://www.cnblogs.com/love201314/p/8489398.html
Copyright © 2020-2023  润新知