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
  • 相关阅读:
    Delphi XE4 FireMonkey 开发 IOS APP 发布到 AppStore 最后一步.
    Native iOS Control Delphi XE4
    Delphi XE4 iAD Framework 支持.
    using IOS API with Delphi XE4
    GoF23种设计模式之行为型模式之命令模式
    Android青翼蝠王之ContentProvider
    Android白眉鹰王之BroadcastReceiver
    Android倚天剑之Notification之亮剑IOS
    Android紫衫龙王之Activity
    GoF23种设计模式之行为型模式之访问者模式
  • 原文地址:https://www.cnblogs.com/love201314/p/8489398.html
Copyright © 2011-2022 走看看