TravestyOfCode
TravestyOfCode
CC#
Created by barcode on 7/2/2023 in #help
❔ EFCore One-To-One
Also could have a table with BusinessId, Day, From, and To columns? Then you can have a relation of one business to many WorkingHours. Would also support having multiple working periods for a business. Say, Monday 9 to 11:30 and 12:30 to 5.
7 replies
CC#
Created by deem on 6/28/2023 in #help
✅ What is the best way to fix StackOverflow when methods are calling each other?
Unless you need to know all possible moves that would result in a Check situation, but it looks like your just doing true/false. Other optimizations could include ignoring a piece's if it's range is outside of king pos. For example, if a pawn is at A2, and King is at G5, there's no need to check that piece for Check as it's impossible.
20 replies
CC#
Created by deem on 6/28/2023 in #help
✅ What is the best way to fix StackOverflow when methods are calling each other?
Quit searching early if you can
20 replies
CC#
Created by deem on 6/28/2023 in #help
✅ What is the best way to fix StackOverflow when methods are calling each other?
It would probably be faster to check each piece by piece instead of getting all valid moves first. Once you find one move that results in a Check (or Checkmate) you don't have to search the other possible moves.
20 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
👍
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
I think. Might need to tweak the Select stuff, though as I may have typos.
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
_dbContext.Product
.Include(p => p.ProductRating)
.Select(p => new Product()
{
Id = p.Id,
Name = p.Name,
AverageRating = p.ProductRating.Select(r => r.Rating).DefaultIfEmpty().Average(),
UserRating = userId == null ? null : p.ProductRating.FirstOrDefault(pr => pr.UserId == userId)?.Rating
}).FirstOrDefault();
_dbContext.Product
.Include(p => p.ProductRating)
.Select(p => new Product()
{
Id = p.Id,
Name = p.Name,
AverageRating = p.ProductRating.Select(r => r.Rating).DefaultIfEmpty().Average(),
UserRating = userId == null ? null : p.ProductRating.FirstOrDefault(pr => pr.UserId == userId)?.Rating
}).FirstOrDefault();
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
So you're still going to want to have the navigation property on Product to link it to all of the ProductRating. Then something like:
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
So ProductResponse will have public int? UserRating { get; set; } which is where you want to have the ProductRating.Rating value stored if the user has rated the product, yes?
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
Your ProductResponse has no idea about ratings, though.
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
In ToContract you would need to check for a null ProductRating as part of the product. If you have a int? called UserRating you would add if (product.ProductRating != null) UserRating = product.ProductRating.Rating; to your ToContract
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
Does your ProductResponse have a property for the user rating, either an int? or a ProductRatingResponse object?
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
You can use projection in the query to map a domain model to another type. It looks something like:
return await _dbContext.Product
.Include(p => p.ProductRating)
.Where(p => p.Id == id && p => p.ProductRating.UserId == userId)
.Select(p => new ProductResponse()
{
Id = p.Id,
Name = p.Name,
// etc.
UserRating = p.ProductRating.Rating
}
.FirstOrDefaultAsync(token);
return await _dbContext.Product
.Include(p => p.ProductRating)
.Where(p => p.Id == id && p => p.ProductRating.UserId == userId)
.Select(p => new ProductResponse()
{
Id = p.Id,
Name = p.Name,
// etc.
UserRating = p.ProductRating.Rating
}
.FirstOrDefaultAsync(token);
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
Oh, like DTO to domain?
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
They're called Navigation properties, they don't actually create a column in your database as long as you've defined them as part of the relation.
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
The extra property? You mean the ProductRatings on the Product class and Product on the ProductRating table?
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
(code might need some cleanup as I just typed it in discord and not IDE)
44 replies
CC#
Created by Dropps on 6/27/2023 in #help
❔ efcore querying
Yeah, the User was only if you wanted to navigate to your user from ProductRating otherwise you can exclude it. Then you can query something like:
if (userId == null)
{
return await _dbContext.Product.FirstOrDefaultAsync(f => f.Id == id, token);
}
else
{
return await _dbContext.Product
.Include(p => p.ProductRating)
.Where(p => p.Id == id && p => p.ProductRating.UserId == userId)
.FirstOrDefaultAsync(token);
}
if (userId == null)
{
return await _dbContext.Product.FirstOrDefaultAsync(f => f.Id == id, token);
}
else
{
return await _dbContext.Product
.Include(p => p.ProductRating)
.Where(p => p.Id == id && p => p.ProductRating.UserId == userId)
.FirstOrDefaultAsync(token);
}
44 replies