Entity Framework Mysql Slow Query - mysql
In my database I have 3 main classes which are Breeders, Horses and Races. These 3 tables have many-to-many relationships. A breeder can have multiple horses and a horse can have multiple breeders. Also a horse can enter multiple races and a race can have multiple horses entered. Now I want to query a breeders race stats. Using entity framework I have this line of code:
var races = DbContext.HorseBreeders
.Where(w => w.BreederId == someint)
.SelectMany(s => s.Horse.Races)
.ToList();
and this code generates this mysql query:
SELECT `w.Horse.Races`.`Id`, `w.Horse.Races`.`FinishTime`, `w.Horse.Races`.`Horse_Id`, `w.Horse.Races`.`Race_Id`
FROM `HorseBreeders` AS `w`
INNER JOIN `Horses` AS `w.Horse` ON `w`.`HorseId` = `w.Horse`.`Id`
INNER JOIN `RaceEntries` AS `w.Horse.Races` ON `w.Horse`.`Id` = `w.Horse.Races`.`Horse_Id`
WHERE `w`.`BreederId` = someint
and this query takes around 30 seconds.
Breeders table has 13k records
Horses table has 60k records
HorseBreeders table has 40k records
Races table has 110k records
RaceEntries table has 960k records.
In Mssql this same structure with a similar query was taking less than a second. But in Mysql its taking too much time. What am I doing wrong?
DbContext Classes:
public class Breeder
{
public int Id { get; set; }
[StringLength(50)]
public string Name { get; set; }
public virtual ICollection<HorseBreeder> Horses { get; set; }
}
public class Horse
{
public int Id { get; set; }
[StringLength(50)]
public string Name { get; set; }
public int? Father_Id { get; set; }
public virtual Horse Father { get; set; }
public int? Mother_Id { get; set; }
public virtual Horse Mother { get; set; }
public string BirthPlace { get; set; }
public DateTime? BirthDate { get; set; }
public virtual ICollection<HorseBreeder> Breeders { get; set; }
public virtual ICollection<RaceEntry> Races { get; set; }
public virtual ICollection<Horse> FatherChilds { get; set; }
public virtual ICollection<Horse> MotherChilds { get; set; }
}
public class HorseBreeder
{
public int HorseId { get; set; }
public Horse Horse { get; set; }
public int BreederId { get; set; }
public Breeder Breeder { get; set; }
}
public class Race
{
public int Id { get; set; }
public DateTime Time { get; set; }
public string Name { get; set; }
public virtual ICollection<RaceEntry> Horses { get; set; }
}
public class RaceEntry
{
public int Id { get; set; }
public int Race_Id { get; set; }
public virtual Race Race { get; set; }
public int Horse_Id { get; set; }
public virtual Horse Horse { get; set; }
public short? FinishTime { get; set; }
}
DbContext OnModelCreating:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Horse
builder.Entity<HorseBreeder>()
.HasKey(bc => new { bc.HorseId, bc.BreederId });
builder.Entity<HorseBreeder>()
.HasOne(bc => bc.Horse)
.WithMany(b => b.Breeders)
.HasForeignKey(bc => bc.HorseId);
builder.Entity<HorseBreeder>()
.HasOne(bc => bc.Breeder)
.WithMany(c => c.Horses)
.HasForeignKey(bc => bc.BreederId);
builder.Entity<Horse>()
.HasOne(p => p.Father)
.WithMany(p => p.FatherChilds)
.HasForeignKey(p => p.Father_Id);
builder.Entity<Horse>()
.HasOne(p => p.Mother)
.WithMany(p => p.MotherChilds)
.HasForeignKey(p => p.Mother_Id);
// RaceEntry
builder.Entity<RaceEntry>()
.HasOne(m => m.Race)
.WithMany(t => t.Horses)
.HasForeignKey(m => m.Race_Id);
builder.Entity<RaceEntry>()
.HasOne(m => m.Horse)
.WithMany(t => t.Races)
.HasForeignKey(m => m.Horse_Id);
}
Database create migration:
public partial class initial_create : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Breeders",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(maxLength: 50, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Breeders", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Horses",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(maxLength: 50, nullable: true),
Father_Id = table.Column<int>(nullable: true),
Mother_Id = table.Column<int>(nullable: true),
BirthPlace = table.Column<string>(nullable: true),
BirthDate = table.Column<DateTime>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Horses", x => x.Id);
table.ForeignKey(
name: "FK_Horses_Horses_Father_Id",
column: x => x.Father_Id,
principalTable: "Horses",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Horses_Horses_Mother_Id",
column: x => x.Mother_Id,
principalTable: "Horses",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "Races",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Time = table.Column<DateTime>(nullable: false),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Races", x => x.Id);
});
migrationBuilder.CreateTable(
name: "HorseBreeders",
columns: table => new
{
HorseId = table.Column<int>(nullable: false),
BreederId = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_HorseBreeders", x => new { x.HorseId, x.BreederId });
table.ForeignKey(
name: "FK_HorseBreeders_Breeders_BreederId",
column: x => x.BreederId,
principalTable: "Breeders",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_HorseBreeders_Horses_HorseId",
column: x => x.HorseId,
principalTable: "Horses",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "RaceEntries",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Race_Id = table.Column<int>(nullable: false),
Horse_Id = table.Column<int>(nullable: false),
FinishTime = table.Column<short>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_RaceEntries", x => x.Id);
table.ForeignKey(
name: "FK_RaceEntries_Horses_Horse_Id",
column: x => x.Horse_Id,
principalTable: "Horses",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_RaceEntries_Races_Race_Id",
column: x => x.Race_Id,
principalTable: "Races",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_HorseBreeders_BreederId",
table: "HorseBreeders",
column: "BreederId");
migrationBuilder.CreateIndex(
name: "IX_Horses_Father_Id",
table: "Horses",
column: "Father_Id");
migrationBuilder.CreateIndex(
name: "IX_Horses_Mother_Id",
table: "Horses",
column: "Mother_Id");
migrationBuilder.CreateIndex(
name: "IX_RaceEntries_Horse_Id",
table: "RaceEntries",
column: "Horse_Id");
migrationBuilder.CreateIndex(
name: "IX_RaceEntries_Race_Id",
table: "RaceEntries",
column: "Race_Id");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "HorseBreeders");
migrationBuilder.DropTable(
name: "RaceEntries");
migrationBuilder.DropTable(
name: "Breeders");
migrationBuilder.DropTable(
name: "Horses");
migrationBuilder.DropTable(
name: "Races");
}
}
Related
Prisma One-to-One update Parent or update Parent and Child
I have a Parent Child (One-To-One) Relationship like this: model Account { id Int #id #default(autoincrement()) createdAt DateTime #default(now()) updatedAt DateTime #updatedAt billingAddress Address? name String ##map("Accounts") } model Address { id Int #id #default(autoincrement()) city String? country String? postalCode Int? state String? street String? accountId Int #unique account Account #relation(fields: [accountId], references: [id]) } I want to be able to Update the Parent Record without the need of updating also the Child Record. Furthermore, it would be great, if I can update the Parent Record and the Child Record at the same time. Right now I am getting an Error when only trying to send the Data for the Parent Record. Here are my DTOs to Create and Edit the Entities: Create / Edit Account: export class CreateAccountDto { #IsString() #IsOptional() name: string; #IsOptional() billingAddress?: CreateAddressDto; } Create / Edit Addresss: export class EditAddressDto { #IsString() #IsOptional() city?: string; #IsString() #IsOptional() country?: string; #IsNumber() #IsOptional() postalCode?: number; #IsString() #IsOptional() state?: string; #IsString() #IsOptional() street?: string; #IsInt() #IsOptional() accountId: number; } I'm creating and editing the Account like this: async editAccount(accountId: number, dto: EditAccountDto) { let account; console.log({dto}) account = await this.prisma.account.update({ where: { id: accountId }, data: { ...dto, billingAddress: { update: { ...dto.billingAddress } } }, include: { billingAddress: true } }); console.log(account) return account; } When i try to Edit the Account with the following Data { "name": "Test Account Create2", "billingAddress": { "id": 2, "city": "Dortmund", "state": "NRW", "postalCode": 44442, "country": "Germany", "street": "Benninghofer Heide 63", "accountId": 10000001 } } i am getting the following Error: Unknown arg `accountId` in data.billingAddress.update.accountId for type AddressUncheckedUpdateWithoutAccountInput. Did you mean `country`? Available args: type AddressUncheckedUpdateWithoutAccountInput { id?: Int | IntFieldUpdateOperationsInput city?: String | NullableStringFieldUpdateOperationsInput | Null country?: String | NullableStringFieldUpdateOperationsInput | Null latitude?: Decimal | NullableDecimalFieldUpdateOperationsInput | Null longitude?: Decimal | NullableDecimalFieldUpdateOperationsInput | Null postalCode?: Int | NullableIntFieldUpdateOperationsInput | Null state?: String | NullableStringFieldUpdateOperationsInput | Null street?: String | NullableStringFieldUpdateOperationsInput | Null } at Document.validate (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\#prisma\client\runtime\index.js:29297:20) at serializationFn (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\#prisma\client\runtime\index.js:31876:19) at runInChildSpan (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\#prisma\client\runtime\index.js:25100:12) at PrismaService._executeRequest (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\#prisma\client\runtime\index.js:31883:31) at consumer (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\#prisma\client\runtime\index.js:31810:23) at C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\#prisma\client\runtime\index.js:31815:51 at AsyncResource.runInAsyncScope (node:async_hooks:199:9) at C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\#prisma\client\runtime\index.js:31815:29 at runInChildSpan (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\#prisma\client\runtime\index.js:25100:12) at PrismaService._request (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\#prisma\client\runtime\index.js:31812:22)
The error says, you are not allowed to specify accountId when you are updating the address in this way. You can just remove it from your DTO and everything should be fine.
.Net 3.1 DataSeeding order of operation problem
I'm trying to seed a database with about 1000 users and agents and about 5000 posts. I have added the users and agents but when I try to add the posts the migration gets created normally but the update-database command return an error. I tried adding a user manually, created a post with the users id and added a migration just for the post and it worked. I think that this is a order of operations problem. I'm thinking that when the post is being inserted to the database it cannot find the user it is looking for because the user is not in the database yet. Any help is greatly appreciated. Thank you for your time in advance <3. The error reads: Build started... Build succeeded. fail: Microsoft.EntityFrameworkCore.Database.Command[20102] Failed executing DbCommand (25ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'Baths', N'Beds', N'BuildingType', N'CategoryId', N'City', N'County', N'CreatedAt', N'Description', N'HouseNumber', N'IsActive', N'IsDeleted', N'IsPrioritize', N'IsPublished', N'Latitude', N'Longitute', N'OwnerId', N'PostalCode', N'Price', N'Size', N'State', N'Street', N'StreetNumber', N'Title') AND [object_id] = OBJECT_ID(N'[Post]')) SET IDENTITY_INSERT [Post] ON; INSERT INTO [Post] ([Id], [Baths], [Beds], [BuildingType], [CategoryId], [City], [County], [CreatedAt], [Description], [HouseNumber], [IsActive], [IsDeleted], [IsPrioritize], [IsPublished], [Latitude], [Longitute], [OwnerId], [PostalCode], [Price], [Size], [State], [Street], [StreetNumber], [Title]) VALUES (1, 3, 5, 1, 1, N'New York', N'New Work', '2022-08-04T10:58:44.5986172+02:00', N'We are selling a house blla blla', 15, CAST(0 AS bit), CAST(0 AS bit), CAST(0 AS bit), CAST(1 AS bit), 30.899999999999999E0, 15.5E0, N'00e0ad26-5df2-45a8-a929-dde8f9c429c3', N'15863', 150000.0E0, 150, N'New York', N'83rd Street', 186, N'Selling House'); IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'Baths', N'Beds', N'BuildingType', N'CategoryId', N'City', N'County', N'CreatedAt', N'Description', N'HouseNumber', N'IsActive', N'IsDeleted', N'IsPrioritize', N'IsPublished', N'Latitude', N'Longitute', N'OwnerId', N'PostalCode', N'Price', N'Size', N'State', N'Street', N'StreetNumber', N'Title') AND [object_id] = OBJECT_ID(N'[Post]')) SET IDENTITY_INSERT [Post] OFF; Failed executing DbCommand (25ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'Baths', N'Beds', N'BuildingType', N'CategoryId', N'City', N'County', N'CreatedAt', N'Description', N'HouseNumber', N'IsActive', N'IsDeleted', N'IsPrioritize', N'IsPublished', N'Latitude', N'Longitute', N'OwnerId', N'PostalCode', N'Price', N'Size', N'State', N'Street', N'StreetNumber', N'Title') AND [object_id] = OBJECT_ID(N'[Post]')) SET IDENTITY_INSERT [Post] ON; INSERT INTO [Post] ([Id], [Baths], [Beds], [BuildingType], [CategoryId], [City], [County], [CreatedAt], [Description], [HouseNumber], [IsActive], [IsDeleted], [IsPrioritize], [IsPublished], [Latitude], [Longitute], [OwnerId], [PostalCode], [Price], [Size], [State], [Street], [StreetNumber], [Title]) VALUES (1, 3, 5, 1, 1, N'New York', N'New Work', '2022-08-04T10:58:44.5986172+02:00', N'We are selling a house blla blla', 15, CAST(0 AS bit), CAST(0 AS bit), CAST(0 AS bit), CAST(1 AS bit), 30.899999999999999E0, 15.5E0, N'00e0ad26-5df2-45a8-a929-dde8f9c429c3', N'15863', 150000.0E0, 150, N'New York', N'83rd Street', 186, N'Selling House'); IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'Baths', N'Beds', N'BuildingType', N'CategoryId', N'City', N'County', N'CreatedAt', N'Description', N'HouseNumber', N'IsActive', N'IsDeleted', N'IsPrioritize', N'IsPublished', N'Latitude', N'Longitute', N'OwnerId', N'PostalCode', N'Price', N'Size', N'State', N'Street', N'StreetNumber', N'Title') AND [object_id] = OBJECT_ID(N'[Post]')) SET IDENTITY_INSERT [Post] OFF; Microsoft.Data.SqlClient.SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Post_AspNetUsers_OwnerId". The conflict occurred in database "Team3UberMain", table "dbo.AspNetUsers", column 'Id'. The statement has been terminated. at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite) at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName) at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection) at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) ClientConnectionId:4e393a1b-a11d-4a8d-a2bb-646dd3c14e58 Error Number:547,State:0,Class:16 The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Post_AspNetUsers_OwnerId". The conflict occurred in database "Team3UberMain", table "dbo.AspNetUsers", column 'Id'. The statement has been terminated. PM> Add-Migration Init Build started... Build succeeded. To undo this action, use Remove-Migration. This is the class responsible for seeding the data (focus on AddUsers and AddPosts) : public static class AddData { private static List<string> NamesList { get { return new List<string>("Liam,Noah,Oliver,Elijah,James,...,William,Benjamin".Split(",")); } } private static List<string> GetSurnamesList(int i = 0) { string[] surnames = "Smith,Johnson,Williams,Brown,...,Tang,Archer".Split(","); if (i > 0) Rotate(ref surnames, i); return new List<string>(surnames); } private static int NrOfUsers = 0; public static void AddUsers(ModelBuilder mb){ List<string> Names = NamesList; List<string> Surnames = GetSurnamesList(); List<User> UsersList = new List<User>(); for (int i = 0; i < Names.Count; i++) { string name = Names[i]; string surname = Surnames[i]; string phoneNumber = "0"+(44000000+i+NrOfUsers); DateTime dateTime = DateTime.Now; int userId = i+NrOfUsers; string emailDomain = i % 3 == 0 ? "#gmail.com" : (i % 3 == 1 ? "#hotmail.com" : "#outlook.com"); string email = name + surname + emailDomain; string password = name +surname +"123."; string hash = HashPassword(password); User u = new User { Email = email, NormalizedEmail = email.ToUpper(), EmailConfirmed = true, UserName = email, NormalizedUserName = email.ToUpper(), UserId = userId, FirstName = name, LastName = surname, Password = password, PasswordHash = hash, ConfirmPassword = password, PhoneNumber = phoneNumber, DateOfBirth = dateTime, LockoutEnabled = true, IsDeleted = false, }; UsersList.Add(u); } mb.Entity<User>().HasData(UsersList); NrOfUsers += Names.Count; } public static void AddAgents(ModelBuilder mb) { List<string> Names = NamesList; List<string> Surnames = GetSurnamesList(1); List<Agent> AgentsList = new List<Agent>(); for (int i = 0; i < Names.Count; i++) { string name = Names[i]; string surname = Surnames[i]; string phoneNumber = "0" + (44555000 + i+ NrOfUsers); DateTime dateTime = DateTime.Now; int userId = i+NrOfUsers; string emailDomain = i % 3 == 0 ? "#gmail.com" : (i % 3 == 1 ? "#hotmail.com" : "#outlook.com"); string email = name + surname + emailDomain; string password = name + surname + "123."; string hash = HashPassword(password); Agent a = new Agent { Email = email, NormalizedEmail = email.ToUpper(), EmailConfirmed = true, UserName = email, NormalizedUserName = email.ToUpper(), UserId = userId, FirstName = name, LastName = surname, Password = password, PasswordHash = hash, ConfirmPassword = password, PhoneNumber = phoneNumber, DateOfBirth = dateTime, LockoutEnabled = true, IsDeleted = false, AgentId = 100000 + i + NrOfUsers }; AgentsList.Add(a); } mb.Entity<Agent>().HasData(AgentsList); NrOfUsers += Names.Count; } public static void AddCategorys(ModelBuilder mb) { mb.Entity<Category>().HasData( new Category() { Id = 1, CategoryName = "Land", }, new Category() { Id = 2, CategoryName = "Apartment" }, new Category() { Id = 3, CategoryName = "House" }, new Category() { Id = 4, CategoryName = "Farm" }, new Category() { Id = 5, CategoryName = "Barn" }, new Category() { Id = 6, CategoryName = "Flat" }, new Category() { Id = 7, CategoryName = "Penthouse" } ) ; } public static void AddPosts(ModelBuilder mb) { mb.Entity<Post>().HasData( new Post { Id = 1, Title = "Selling House", Price = 150000, State = "New York", City = "New York", PostalCode = "15863", Street = "83rd Street", HouseNumber = 15, StreetNumber = 186, County = "New Work", CategoryId = 1, // // todo : mi ndrreq owner id se osht string i gat OwnerId = "00e0ad26-5df2-45a8-a929-dde8f9c429c3", Description = "We are selling a house blla blla", Longitute = 15.5, Latitude = 30.9, BuildingType = 1, Size = 150, Beds = 5, Baths = 3, CreatedAt = DateTime.Now, IsPrioritize = false, // TODO: add images and sponsored IsPublished = true } ); } private static void Rotate<T>(ref T[] array, int shiftCount) { if (shiftCount == 0) return; T[] backupArray = new T[array.Length]; for (int index = 0; index < array.Length; index++) { backupArray[(index + array.Length + shiftCount % array.Length) % array.Length] = array[index]; } array = backupArray; } private static string HashPassword(string password) { byte[] salt; byte[] buffer2; if (password == null) { throw new ArgumentNullException("password"); } using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, 0x10, 0x3e8)) { salt = bytes.Salt; buffer2 = bytes.GetBytes(0x20); } byte[] dst = new byte[0x31]; Buffer.BlockCopy(salt, 0, dst, 1, 0x10); Buffer.BlockCopy(buffer2, 0, dst, 0x11, 0x20); return Convert.ToBase64String(dst); } } This are the user and post models: public class User : IdentityUser { public int UserId { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } [Required] public string Password { get; set; } [Required] public string ConfirmPassword { get; set; } [Required] public DateTime DateOfBirth { get; set; } [Required] public bool IsDeleted { get; set; } [Required] public bool IsActive { get; set; } public List<Post> Posts { get; set; } //public Guid ResetToken { get; set; } } public class Post : BaseAttributes { [Key] public int Id { get; set; } [Required] public string Title { get; set; } public double Price { get; set; } [Required] public string State { get; set; } [Required] public string City { get; set; } public string PostalCode { get; set; } public string Street { get; set; } public int HouseNumber { get; set; } public int StreetNumber { get; set; } public string County { get; set; } public int? CategoryId { get; set; } [ForeignKey("CategoryId")] public Category Category { get; set; } public string OwnerId { get; set; } [ForeignKey("OwnerId")] public User Owner { get; set; } [Required] public string Description { get; set; } [Required] public double Longitute { get; set; } [Required] public double Latitude { get; set; } [Required] public int BuildingType { get; set; } [Required] public int Size { get; set; } [Required] public int Beds { get; set; } [Required] public int Baths { get; set; } [Required] public DateTime CreatedAt { get; set; } = DateTime.Now; public bool IsPrioritize { get; set; } public List<Image> Images { get; set; } public List<Sponsored> Sponsored { get; set; } public bool IsPublished { get; set; } = false; public virtual ICollection<Taken_By_Relation> Taken_by_list { get; set; } } This is the ApplicationDbContext: public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<User> User { get; set; } public DbSet<Agent> Agent { get; set; } public DbSet<SimpleUser> SimpleUser { get; set; } public DbSet<Post> Post { get; set; } public DbSet<Image> Image { get; set; } public DbSet<Category> Category { get; set; } public DbSet<Agent_Post_CounterOffer> Agent_Post_CounterOffer { get; set; } public DbSet<CounterOffer> CounterOffer { get; set; } public DbSet<FeedBack> FeedBack { get; set; } public DbSet<Salary> Salary { get; set; } public DbSet<Taken_By_Relation> Taken_By_Relation { get; set; } public DbSet<Given_By_Relation> Given_By_Relation { get; set; } public DbSet<Sponsored> Sponsored{ get; set; } //public DbSet<Invite> Invite{ get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { AddData.AddUsers(modelBuilder); AddData.AddAgents(modelBuilder); AddData.AddCategorys(modelBuilder); AddData.AddPosts(modelBuilder); base.OnModelCreating(modelBuilder); modelBuilder.Entity<Agent_Post_CounterOffer>().HasNoKey(); modelBuilder.Entity<Given_By_Relation>().HasNoKey(); modelBuilder.Entity<ChangePasswordViewModel>().HasNoKey(); modelBuilder.Entity<Salary>().HasNoKey(); //modelBuilder.Entity<Agent_Post_CounterOffer>().HasKey(ap => new { ap.AgentId, ap.PostId, ap.CounterOfferId }); //modelBuilder.Entity<Given_By_Relation>().HasKey(ap => new { ap.Given_By, ap.JobId, ap.PostId }); //modelBuilder.Entity<Taken_By_Relation>().HasKey(ap => new { ap.Taken_By, ap.JobId, ap.PostId }); } }
How to restructure an Object in C# using LINQ?
I have a data set as follows: [ { "Id": 1, "Country": "Uruguay", "Name": "Foo", "Status": "Completed", }, { "Id": 2, "Country": "Uruguay", "Name": "Foo", "Status": "Completed", }, { "Id": 3, "Country": "Germany", "Name": "Foo", "Status": "Completed", }, ] I want to transform and sort it by Country so that it looks as follows: [ { "Country": "Uruguay", "Details": [ { "Id": 1, "Name": "Foo", "Status": "Completed", }, { "Id": 2, "Name": "Foo", "Status": "Completed", }, ], }, { "Country": "Germany", "Details": [ { "Id": 3, "Name": "Foo", "Status": "Completed", }, ], }, ], These are the classes in C#: public class Countries { public int Id { get; set; } public string Country { get; set; } public string Name { get; set; } public string Status { get; set; } } public class Details { public int Id { get; set; } public string Name { get; set; } public string Status { get; set; } } public class CountryList { public string Country { get; set; } public List<Details> Details { get; set; } } Some of what I have tried looks as followed: var foo = countries .GroupBy(x => new Details { Id = x.Id, Name = x.Name, Status = x.Status }) .Select( y => new CountryList { // Country = y.Key. } var foo = countries .GroupBy(x => x.Country) .Select( y => new CountryList { // Country = y.Key. Details = y.GroupBy(a => new Details { Id = a.Id, Name = a.Name, Status = a.Status }).ToList() } I am having trouble working out how to use LINQ to solve this. I have done a handful of GroupBy operations in the past, but I wasn't able to work this one out. How do I transform my dataset into the desired result?
You do not need second GroupBy var foo = countries .GroupBy(x => x.Country) .Select(y => new CountryList { Country = y.Key, Details = y.Select(a => new Details { Id = a.Id, Name = a.Name, Status = a.Status }).ToList() };
You can take advantage of the .GroupBy() overload that lets you define a resultSelector to create your CountryLists and populate their Details: var countries = new List<Countries> { new() { Id = 1, Country = "Uruguay", Name = "Foo", Status = "Completed" }, new() { Id = 2, Country = "Uruguay", Name = "Foo", Status = "Completed" }, new() { Id = 3, Country = "Germany", Name = "Foo", Status = "Completed" }, }; List<CountryList> countryList = countries .GroupBy( c => c.Country, ( country, matches ) => new CountryList() { Country = country, Details = matches.Select(match => new Details { Id = match.Id, Name = match.Name, Status = match.Status }).ToList() }) .ToList(); , ( country, matches ) => new CountryList() { ... } being the resultSelector. Example fiddle here.
try this var orig = JsonConvert.DeserializeObject<List<Countries>>(json); List<CountryList> countries = orig.GroupBy(o => o.Country) .Select(x => new CountryList { Country = x.Key, Details = x.Select(o => new Details {Id=o.Id,Name=o.Name,Status=o.Status} ).ToList() }).ToList();
TypeORM relationship: how to get Entites from relations with custom properties Table
i have a many-to-many relations with custom properties. from https://github.com/typeorm/typeorm/blob/master/docs/many-to-many-relations.md Now i like to get the related Entities. How to Join Table or Column of relations Table? #Entity({name: 'a'}) export class A { #PrimaryGeneratedColumn() id: number; #OneToMany(type => AB, ab => ab.a, ) abs: ab[]; ??? // how to get array of B here? b: B[]; } #Entity({name: 'b'}) export class B { #PrimaryGeneratedColumn() id: number; #OneToMany(type => AB, ab => ab.b, ) abs: ab[]; } #Entity() export class AB { #PrimaryGeneratedColumn() id: number; #Column() public aId!: number; #Column() public bId!: number; #Column() public position!: number; #ManyToOne(type => A, a => a.abs,) public a!: A; #ManyToOne(type => B,b => b.abs) public b!: B; } Database | A | B | AB ---------------------------------- |id | id | id, aId, bId. position response should be a = { id: 1, abs: [ { "id": 1, "aId": 1, "bId": 1, "position": 1 } ], b:[{"id: 1"}] // need this }
How to Fetch data from two tables in cakephp 3
I am new in cakephp and my table like: city id | name 1 | city1 2 | city2 state id | name | cityid 1 |state1| 2 so how do i get the city name if i having state id. In controller i have code like this. public function getCity() { if( $this->request->is('ajax') ) { $this->autoRender = false; } if ($this->request->isPost()) { $sId= $this->request->data['stateid']; } } In the $sId i get value so how do i write query.
If you have a BelongsTo relationship between both Model, you just have to do a query on the States which contains City: public function getCity() { if( $this->request->is('ajax') ) { $this->autoRender = false; } if ($this->request->isPost()) { $stateEntity = $this->States->find('all') ->where(['id' => $this->request->data['stateid']]) ->contain(['Cities']) ->first(); // Now the State Object contains City $cityName = $stateEntity->city->name; } } To create this relationship you need to do like this: class StatesTable extends Table { public function initialize(array $config) { $this->belongsTo('Cities') ->setForeignKey('city_id') ->setJoinType('INNER'); } }