Create unique postalcode and country generator - c#

I am working an C# application where I have to add test employee data to a MongoDB collection.
There is a working connection already and some predefined list where the program is going to loop through to insert data. However, I am stuck add the point where I'd have to add an address.
I need a piece of code to create at least 10.000 unique postalcodes without using a library if possible.
using System;
using System.Collections.Generic;
using MongoDB.Driver;
using MongoDB.Bson;
namespace Application
{
class Program
{
public class Employee
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Street { get; set; }
public string Country { get; set; }
public string Postalcode { get; set; }
}
static void Main(string[] args)
{
MongoClient client = new MongoClient();
var server = client.GetServer();
var db = server.GetDatabase("testdb");
var collection = db.GetCollection<Employee>("testcollection");
List<string> names = new List<string>()
{
"John Smith",
"Matthew Williams",
"David Harris",
"Christopher Martin",
"Paul Shark"
};
Random random = new Random();
for (int i = 0; i < 5; i++)
{
int nameIndex = random.Next(0, 5);
int projectIndex = random.Next(0, 3);
int ageIndex = random.Next(18, 67);
string nameValue = names[nameIndex];
Employee employee = new Employee
{
BSN = "BSN" + i,
Name = nameValue,
Age = ageIndex
};
collection.Save(employee);
}
}
}
}

When I test forms I grab Faker.net from Nuget. This will generate random data for everything you need.
The implementation is pretty simple. Going from memory here so your mileage may vary but you get the idea.
var address1 = Faker.address.address1;

Related

I am getting an error while producing objects

I'm developing a project with N-tier architecture. I got errors while generating a new object in the main function.I can not give the features of car1. The entire text is underlined in red and I get these errors "CS1922: Cannot initialize type 'Car' with a collection initializer because it does not implement 'System.Collections.IEnumerable'" and "CS0747: Invalid initializer member declator".
using Business.Concrete;
using System;
using Entities.Concrete;
using DataAccess.Concrete.EntityFramework;
static void Main(string[] args)
{
Car car1 = new Car
{
car1.Id = 4,
car1.BrandId = 1,
car1.ColourId = 2,
car1.ModelYear = 1990,
car1.Name = "King",
car1.DailyPrice = 150,
car1.Description = "Best"
};
CarManager carManager = new CarManager(new EfCarDal());
Console.ReadLine();
}
Entities\Concrete\Car.cs
using Core.Entities;
using System;
using System.Collections.Generic;
using System.Text;
namespace Entities.Concrete
{
public class Car : IEntity
{
public int Id { get; set; }
public int BrandId { get; set; }
public int ColourId { get; set; }
public string Name { get; set; }
public int ModelYear { get; set; }
public int DailyPrice { get; set; }
public string Description { get; set; }
}
}
You don't need the car1. access when initializing within {} like so:
Car car1 = new Car
{
Id = 4,
BrandId = 1,
ColourId = 2,
ModelYear = 1990,
Name = "King",
DailyPrice = 150,
Description = "Best"
};

Query MongoDB in C# using LINQ

I have simple class which represents fields in MongoDB document
class Measurement
{
public ObjectId id { get; set; }
public int s { get; set; }
public int[] p { get; set; }
public int dt { get; set; }
public int ml { get; set; }
}
I'm trying to get documents matching my conditions using
var collection = database.GetCollection<Measurement>(mongoCollectionName);
var query = from a in collection.AsQueryable<Measurement>()
where a.dt > 100
select a;
When where condition is removed i do receive all documents but with condition none. Response says there's no matching documents but there are (example dt=1538555)
query looks like this {aggregate([{ "$match" : { "dt" : { "$gt" : 100 } } }])}.
I build my example using response from this thread and mongodb documentation
MongoDB C# Aggregation with LINQ
I would be grateful with solving probably my stupid mistake
i don't use the c# driver directly anymore so my solution is using MongoDAL.
using System;
using System.Linq;
using MongoDAL;
namespace Example
{
class Measurement : Entity
{
public int s { get; set; }
public int[] p { get; set; }
public int dt { get; set; }
public int ml { get; set; }
}
class Program
{
static void Main(string[] args)
{
new DB("measurements");
var measurement1 = new Measurement
{
s = 10,
ml = 20,
dt = 100,
p = new int[] { 1, 2, 3, 4, 5 }
};
var measurement2 = new Measurement
{
s = 11,
ml = 22,
dt = 200,
p = new int[] { 1, 2, 3, 4, 5 }
};
measurement1.Save();
measurement2.Save();
var result = (from m in DB.Collection<Measurement>()
where m.dt > 100
select m).ToArray();
Console.ReadKey();
}
}
}

Making random number in Linq [duplicate]

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 5 years ago.
Hi i writing this below code for make some person. in query i want to get all persons and select instance form every person this below code is for Main method:
Persons[] person = { new Persons(534, "Komeil", "Shahmoradi", 20, 1.65f, 68.3f),
new Persons(1047, "Ahmad", "Darvishi", 18, 1.72f, 70) ,
new Persons(56, "Javad", "Amini", 28, 1.56f, 73.2f),
new Persons(2, "Hossein", "Kiany", 17, 1.80f, 65.6f) ,
new Persons(192, "Hossein", "Kazemy", 15, 1.80f, 83.6f),
new Persons(2002, "Hossein", "Saeedi", 43, 1.80f, 93.6f)};
Random rnd = new Random();
var QpersonsLocation = from value in person
select new Duty(rnd.Next(1,2000), value.pName, value.pFamily, value.pAge);
Console.WriteLine("[/] System seting location");
foreach (var item in QpersonsLocation)
{
Console.WriteLine(item.dId +"\t" + item.dName + "\t" + item.dFamily + "\t" + item.dAge + "\t" + item.dDuttyLocation+"\t");
}
And this is for my Persons class:
class Persons
{
public long pId { get; set; }
public string pName { get; set; }
public string pFamily { get; set; }
public byte pAge { get; set; }
public float pSize { get; set; }
public float pWeight { get; set; }
public Persons(long pId, string pName, string pFamily, byte pAge, float pSize, float pWeight)
{
this.pId = pId;
this.pName = pName;
this.pFamily = pFamily;
this.pAge = pAge;
this.pSize = pSize;
this.pWeight = pWeight;
}
}
problem in the lass code for Duty class :
class Duty
{
string[] locations = {"Esfahan",
"Tehran",
"Mashhad",
"Shiraz",
"Hamedan",
"Azarbayejan"};
public long dId {get;set;}
public string dName { get; set; }
public string dFamily { get; set; }
public byte dAge { get; set; }
public string dDuttyLocation { get; set; }
public Duty(long dId, string dName, string dFamily, byte dAge)
{
this.dId = dId;
this.dName = dName;
this.dFamily = dFamily;
this.dAge = dAge;
Random rnd = new Random();
int num = rnd.Next(0,locations.Length);
dDuttyLocation = locations[num];//problem
}
}
The code working correctly but the location not saving and always dDuttyLocation equals the last number of random number this below picture shows the result:
The problem is creating new instances of the Random class too close in time in your Duty constructor. See here for more info.
Similar to how you're already doing it to generate a new dId for each person, you would need to generate the random number and pass it into the constructor so that they are not all the same.
To do that, you'd first need to make your locations array public and static. Then you can change your LINQ expression to something like:
Random rnd = new Random();
var QpersonsLocation = from value in person
select new Duty(rnd.Next(1,2000), value.pName, value.pFamily, value.pAge)
{
dDuttyLocation = rnd.Next(Duty.locations.Length)
};

C# SQLite Extensions InsertWithChildren doesn't work why?

i have a Problem with my SQLite-Database. I use the SQLite.Net and SQLiteNetExtensions PCL NuGets and followed this excample: Link
I have a connection and it insert a stock but not the valuation list.
My copy paste code:
var valuation1 = new Valuation
{
Price = 15,
Time = DateTime.Now,
};
var valuation2= new Valuation
{
Price = 22,
Time = DateTime.Now,
};
var euro = new Stock
{
Symbol = "€",
Valuations = new List<Valuation> { valuation1, valuation2 }
};
connection.CreateTable<Stock>();
connection.CreateTable<Valuation>();
connection.InsertWithChildren(euro);
var stockList = c.GetAllWithChildren<Stock>();
In Stocklist is a Stock but in Stock is no Valuation :(
usings:
using System; using System.Collections.Generic;
using System.Linq;
using SQLiteNetExtensions.Extensions;
using TestSQLite.Models;
Models:
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;
using System.Collections.Generic;
public class Stock
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(8)]
public string Symbol { get; set; }
[OneToMany] // One to many relationship with Valuation
public List<Valuation> Valuations { get; set; }
}
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;
public class Valuation
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(Stock))] // Specify the foreign key
public int StockId { get; set; }
public DateTime Time { get; set; }
public decimal Price { get; set; }
[ManyToOne] // Many to one relationship with Stock
public Stock Stock { get; set; }
}
Any ideas or suggestions what i could do to make it work?
EDIT:
I just tried some things and if i inseret it by my self like this:
c.Insert(euro);
var e = c.Find<Stock>(s => s.Symbol == "€");
var valuation1 = new Valuation
{
Price = 15,
Time = DateTime.Now,
StockId = e.StockId,
Stock = e
};
var valuation2 = new Valuation
{
Price = 22,
Time = DateTime.Now,
StockId = e.StockId,
Stock = e
};
c.Insert(valuation1);
c.Insert(valuation2);
var stockList = c.GetAllWithChildren<Stock>();
i get a correct stock "euro" and valutations in it! What is my mistake at InsertWithChildren? Or is there anything wrong with my models?

Trouble assigning a value to this variable

Using this code I intend to populate the variables.
http://www.dreamincode.net/forums/xml.php?showuser=146038
//Load comments.
var commentsXML = xml.Element("ipb").Element("profile").Element("comments");
this.Comments = (from comment in commentsXML.Descendants("comment")
select new Comment()
{
ID = comment.Element("id").Value,
Text = comment.Element("text").Value,
Date = comment.Element("date").Value,
UserWhoPosted = comment.Element("user").Value
}).ToList();
The problem is UserWhoPosted is an object of the User.cs class POCO I made:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SharpDIC.Entities
{
public class Friend
{
public string ID { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Photo { get; set; }
}
}
How would I populate that object?
I haven't tested this but can't you instantiate the Friend object when your populating the Comment object?
var commentsXML = xml.Element("ipb").Element("profile").Element("comments");
this.Comments =
(
from comment in commentsXML.Descendants("comment")
select new Comment()
{
ID = comment.Element("id").Value,
Text = comment.Element("text").Value,
Date = comment.Element("date").Value,
UserWhoPosted = new Friend()
{
ID = comment.Element("user").Descendants("id"),
Name = comment.Element("user").Descendants("name"),
Url = comment.Element("user").Descendants("url"),
Photo = comment.Element("user").Descendants("photo")
}
}
).ToList();

Categories

Resources