Answer the question
In order to leave comments, you need to log in
How to project data into a new table?
There is a first model
public class Case
{
public int Id { get; set; }
public string caseName { get; set; }
public int caseNumber { get; set; }
public string caseShortName { get; set; }
public Price Price { get; set; }
}
public class Price
{
public int Id { get; set; }
public double casePrice { get; set; }
public int CaseId { get; set; }
public Case Case { get; set; }
}
public class PriceName
{
public int Id { get; set; }
public string Name { get; set; }
public double TotalPrice { get; set; }
}
public class ApplicationContext : DbContext
{
public DbSet<Case> Cases { get; set; } = null!;
public DbSet<Price> Prices { get; set; } = null!;
public DbSet<PriceName> PriceNames { get; set; } = null!;
public ApplicationContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Server = localhost; Port = 5433; Database = test; Username = postgres; Password = root");
}
}
class Program
{
static void Main(string[] args)
{
using(ApplicationContext db = new ApplicationContext())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
Case riptide = new Case { caseName = "Operation Riptide Case", caseNumber = 176264317, caseShortName = "riptide"};
Case glove = new Case { caseName = "Gloves Case", caseNumber = 175854202, caseShortName = "gloves" };
Case spectrum = new Case { caseName = "Spectrum Case", caseNumber = 175880240, caseShortName = "spectrum" };
Case prisma = new Case { caseName = "Prisma Case", caseNumber = 176042493, caseShortName = "prisma" };
Case fracture = new Case { caseName = "Fracture Case", caseNumber = 176185874, caseShortName = "fracture" };
Case gamma = new Case { caseName = "Gamma Case", caseNumber = 156110183, caseShortName = "gamma" };
Case bloodhound = new Case { caseName = "BloodHound Case", caseNumber = 49359031, caseShortName = "bloodhound" };
Case revolver = new Case { caseName = "Revolver Case", caseNumber = 84444464, caseShortName = "revolver" };
db.Cases.AddRange(riptide, glove, spectrum, prisma, fracture, gamma, bloodhound, revolver);
db.SaveChanges();
Price riptidePrice = new Price { casePrice = 0.58, Case = riptide };
Price glovePrice = new Price { casePrice = 1.84, Case = glove };
Price spectrumPrice = new Price { casePrice = 0.61, Case = spectrum };
Price prismaPrice = new Price { casePrice = 0.07, Case = prisma };
Price fracturePrice = new Price { casePrice = 0.07, Case = fracture };
Price gammaPrice = new Price { casePrice = 0.35, Case = gamma };
Price bloodhoundPrice = new Price { casePrice = 0.50, Case = bloodhound };
Price revolverPrice = new Price { casePrice = 0.12, Case = revolver };
db.Prices.AddRange(riptidePrice, glovePrice, spectrumPrice, prismaPrice, fracturePrice, gammaPrice, bloodhoundPrice, revolverPrice);
db.SaveChanges();
}
}
}
using(ApplicationContext db = new ApplicationContext())
{
var report = db.Cases.Where(x => x.caseShortName == "riptide").Select(s => new PriceName
{
Name = s.caseName,
TotalPrice = s.Price.casePrice
});
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question