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
  • 相关阅读:
    c#正则表达式应用实例
    C# 中堆栈,堆,值类型,引用类型的理解 (摘抄)
    c#用正则表达式获得指定开始和结束字符串中间的一段文本
    asp.net c#截取指定字符串函数
    <收藏>提高Web性能的14条法则(详细版)
    利用Anthem.net 实现前台javascript调用服务器端c#函数 及流程分析
    Anthem.net
    jQuery animate(滑块滑动)
    .NET使用母版页后,控件名称自动生成导致js无法正常操作.net控件的问题
    Cocos2dx跨平台Android环境配置
  • 原文地址:https://www.cnblogs.com/love201314/p/8489398.html
Copyright © 2011-2022 走看看