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
原文地址:https://www.cnblogs.com/love201314/p/8489398.html