来自IQueryable的随机数

时间:2018-12-07 15:05:23

标签: c#

我有一个这样的方法:它是BSN编号的列表。而且我需要有一个随机数。

.dll

从这个数字列表中,我想从这个列表中获得一个随机数。

我这样尝试:

    public virtual IQueryable<TEntity> GetAll()
            {
                return Repository.GetAll();
            }

 public IQueryable<TEntity> GetAll()
        {
            return _dbSet;
        }

但这不起作用。

那么如何归档呢?

谢谢。

如果我这样做的话:

   Random rnd2 = new Random();
     var Banners = HelperManager.LeraarReposHelper.GetAll();//.Where(p => 
     p.PersoonId != 1).OrderBy(x => Guid.NewGuid()).Take(3);
     int index = rnd2.Next(0, Banners.Count() - 1);

    Verwijzing = Banners.ToString()

我收到此错误:

 var Banners = HelperManager.LeraarReposHelper.GetAll().Where(p => p.PersoonId != 1).OrderBy(x => Guid.NewGuid()).Take(1);

这是课程:

 at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at VLR.TestData.Repository.UnitOfWork.SaveChanges() in E:\Projects\Source\Repos\EndToEndTest\VLR.TestData.Repository\UnitOfWork.cs:line 40
   at VLR.TestData.Repository.Helpers.HelperBase`1.Insert(TEntity entity, Boolean autoSave) in E:\Projects\Source\Repos\EndToEndTest\VLR.TestData.Repository\Helpers\HelperBase.cs:line 103
   at VLR.TestData.Repository.Helpers.DBHelpers.ZaakReposHelper.Insert(Zaak zaak, Boolean autoSave) in E:\Projects\Source\Repos\EndToEndTest\VLR.TestData.Repository\Helpers\DBHelpers\ZaakReposHelper.cs:line 42
   at VLR.TestData.Generator.Processes.ZaakGenerator.CreateZaak() in E:\Projects\Source\Repos\EndToEndTest\VLR.TestData.Generator\Processes\ZaakGenerator.cs:line 72
   at VLR.TestData.Generator.Tool.Processes.ZaakProcess.Generate(Int32 size) in E:\Projects\Source\Repos\EndToEndTest\VLR.TestData.Generator.Tool\Processes\ZaakProcess.cs:line 23

这是Leraar类。那么如何解决这个问题。

谢谢

这是课程:

public partial class Leraar : EntityBase
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Leraar()
        {
            this.Bevoegdheid = new HashSet<Bevoegdheid>();
            this.DuoArbeidsovereenkomst = new HashSet<DuoArbeidsovereenkomst>();
            this.LeraarActiefStatusLog = new HashSet<LeraarActiefStatusLog>();
            this.Portfolio = new HashSet<Portfolio>();
            this.Migraties = new HashSet<Migratie>();
            this.AanbodDeelnemers = new HashSet<AanbodDeelnemer>();
            this.LeraarSchools = new HashSet<LeraarSchool>();
            this.PortfolioDoorsturen = new HashSet<PortfolioDoorsturen>();
            Initialize();
        }

        partial void Initialize();

        public int PersoonId { get; set; }
        public int WizardStap { get; set; }
        public Nullable<System.DateTime> RegistratieDatumUtc { get; set; }
        public bool IsGeheim { get; set; }
        public System.Guid LeraarGuid { get; set; }
        public Nullable<System.DateTime> MigratieVoorwaardenAkkoordDatumUtc { get; set; }
        public Nullable<System.Guid> RegisterleraarGebruikerId { get; set; }
        public string RegisterleraarGebruikersnaam { get; set; }
        public string RegisterNummer { get; set; }
        public bool ToonMigratieMelding { get; set; }
        public string Email { get; set; }
        public Nullable<System.DateTime> VerklaringAkkoordDatumUtc { get; set; }
        public Nullable<System.DateTime> AanmeldDatumUtc { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Bevoegdheid> Bevoegdheid { get; set; }
        public virtual BrpPersoon BrpPersoon { get; set; }
        public virtual DuoAdresBuitenland DuoAdresBuitenland { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<DuoArbeidsovereenkomst> DuoArbeidsovereenkomst { get; set; }
        public virtual RegisterVoorportaalTermijn RegisterVoorportaalTermijn { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<LeraarActiefStatusLog> LeraarActiefStatusLog { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Portfolio> Portfolio { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Migratie> Migraties { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<AanbodDeelnemer> AanbodDeelnemers { get; set; }
        public virtual LeraarBeheer LeraarBeheer { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<LeraarSchool> LeraarSchools { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<PortfolioDoorsturen> PortfolioDoorsturen { get; set; }
    }

这是错误消息:

public class ZaakGenerator : DataGenerator
    {

        private BsnDataGenerator _bsnGenerator = new BsnDataGenerator();

        public ZaakGenerator(UnitOfWork unitOfWork) : base(unitOfWork)
        {
            DateTime tomorrow = DateTime.Now;
            var startDate = GetRandomDate(DateTime.Now.AddYears(-3), DateTime.Now.AddDays(-5));
            var endDate = ProbabilityBool(80) ? null : (DateTime?)GetRandomDate(DateTime.Now.AddDays(-1), DateTime.Now.AddYears(2));
        }

        public Zaak CreateZaak()
        {
            ZaakNotitie NotitieZaak = CreateZaakNotitie();
            ZaakHistorie zaakHistory = CreateZaakHistorie();

            //int rnd = new Random();//.Next();
            Random rnd2 = new Random();


            var Banners = HelperManager.LeraarReposHelper.GetAll().Where(p => p.PersoonId != 1).OrderBy(x => Guid.NewGuid()).Take(1).ToList();
            int index = rnd2.Next(0, Banners.Count() - 1);
            int r = rnd2.Next(Banners.Count());


            var numReviews = RandomNumber(1, 100);
            var teachers = HelperManager.LeraarReposHelper.GetAll()
                                .OrderBy(t => Guid.NewGuid())
                                .Take(numReviews)
                                .Select(t => t.PersoonId)
                                .ToList();



            DateTime datumBinnenkomst = GetRandomDate(DateTime.Now.AddMonths(-18), DateTime.Now);
            var DatumAfgehandeld = ProbabilityBool(80) ? null : (DateTime?)GetRandomDate(DateTime.Now.AddDays(-1), DateTime.Now.AddYears(2));
            string bsn = string.Empty;
            var zaakBsn = HelperManager.LeraarReposHelper.GetByBsn(bsn);

            var zaak = new Zaak
            {
                Type = 100,
                Verwijzing = Banners.ToString(), //zaakBsn.ToString(), //_bsnGenerator.GetUniqueBSNNumberFromListZaak(), 
                Status = 103,
                Substatus = null,
                Afhandeling = 106,
                Actiehouder = 101,
                DatumBinnenkomst = datumBinnenkomst,
                DatumAfgehandeld = DatumAfgehandeld,
                Steekproef = generateZeroOrNull(),               

                ZaakNotitie = new[] { NotitieZaak },
                ZaakHistorie = new[] { zaakHistory }

            };
            HelperManager.ZaakReposHelper.Insert(zaak);

            return zaak;

        }
    }

但是BSN号只有9个数字

这是GetBsn方法:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

ErrorMessage = "The field Verwijzing must be a string or array type with a maximum length of '16'."

但是如何从列表中获得一个随机的bsn号呢?

谢谢。

这是GetListAll()的方法:

 public Leraar GetByBsn(string bsn)
        {
            return GetAll().FirstOrDefault(l => l.BrpPersoon.Bsn.Equals(bsn, StringComparison.InvariantCultureIgnoreCase));
        }

所以,如果我这样做:

 public IList<BrpPersoon> GetListAll()
        {
            IQueryable<BrpPersoon> result = GetAll();

            result = result.Where(r => true);

            return result
                .OrderBy(r => r.Bsn)
                .ToList();
        }

然后我会回来:

具有bsn编号的对象。

但是如何只返回一个随机的bsn号码?

谢谢

 public void ReturnAll33()
        {

           var hallo = HelperManager.BrpPersoonReposHelper2.GetListAll()
                .Where(p => p.Bsn != "1")
                .OrderBy(x => Guid.NewGuid())
                .Take(1).ToList();

            Console.WriteLine(hallo);    

        }

然后我得到一个正确的随机bsn数: 309943917。

但是我得到这个错误:

Verwijzing字段必须是最大长度为'16'的字符串或数组类型。

当我评估财产时,像这样:

 public void ReturnAll33()
        {
            var numReviews = RandomNumber(1, 100);

            var hallo = HelperManager.BrpPersoonReposHelper2.GetListAll()
                .Where(p => p.Bsn != "1")
                .OrderBy(x => Guid.NewGuid())
                .Take(numReviews)
                .Select(p => p.Bsn)
                .Take(1).ToList();

            Console.WriteLine(hallo);    

        }

现在看起来像这样:

 var numReviews = RandomNumber(1, 100);

            var selectRandomBsnNumber = HelperManager.BrpPersoonReposHelper2.GetListAll()
                .Where(p => p.Bsn != "1")
                .OrderBy(x => Guid.NewGuid())
                .Take(numReviews)
                .Select(p => p.Bsn)
                .Take(1).ToList();
            //


            ZaakNotitie NotitieZaak = CreateZaakNotitie();
            ZaakHistorie zaakHistory = CreateZaakHistorie();        


            DateTime datumBinnenkomst = GetRandomDate(DateTime.Now.AddMonths(-18), DateTime.Now);
            var DatumAfgehandeld = ProbabilityBool(80) ? null : (DateTime?)GetRandomDate(DateTime.Now.AddDays(-1), DateTime.Now.AddYears(2));         


            var zaak = new Zaak
            {
                Type = 100,
                Verwijzing = selectRandomBsnNumber.ToString(), 
                Status = 103,
                Substatus = null,
                Afhandeling = 106,
                Actiehouder = 101,
                DatumBinnenkomst = datumBinnenkomst,
                DatumAfgehandeld = DatumAfgehandeld,
                Steekproef = generateZeroOrNull(),               

                ZaakNotitie = new[] { NotitieZaak },
                ZaakHistorie = new[] { zaakHistory }

            };
            HelperManager.ZaakReposHelper.Insert(zaak);

1 个答案:

答案 0 :(得分:0)

您显示的代码是Verwijzing = Banners.ToString(),但Banners似乎是一个列表:

var Banners = HelperManager.LeraarReposHelper.GetAll()
                           .Where(p => p.PersoonId != 1)
                           .OrderBy(x => Guid.NewGuid())
                           .Take(1).ToList();

因此,您尝试将ToString的{​​{1}}放入List属性中。

如果我查看您问题的最后一句话,似乎您应该将Verwijzing放到该属性中。

我认为这与随机数无关。