Ok people, before bashing, this is a little bit different from what I've investigated.
I currently have the following code:
public async Task<ParticipantTournament> GetParticipantTournamentByDescending(int tournamentId, int participantId)
{
var response = await _dbRepositories.TournamentParticipantMatchRepository
.Where(x => x.TournamentId == tournamentId)
.OrderByDescending(y => y.TournamentMatch.RoundNumber)
.ThenByDescending(y => y.TournamentMatch.Id)
.Include(x => x.Tournament)
.Include(x => x.Participant1)
.Include(x => x.Participant2)
.Include(x => x.TournamentMatch)
.Select(z => new TournamentParticipantMatchLogicDto
{
IsLastMatch = OneTrue() // <==== Problem here,
TournamentParticipantMatch = z
}).Where(x => (x.TournamentParticipantMatch.Participant1Id == participantId || x.TournamentParticipantMatch.Participant2Id == participantId))
.ToListAsync();
return new ParticipantTournament
{
ParticipantMatches = response,
};
}
/**
* This may be something super dumb. But I couldnt' come up with something better.
* How do I detect the latest match?
* http://stackoverflow.com/questions/14687136/check-if-record-is-last-or-first-in-list-with-linq
*
* Well, after digging a little bit, I've found that this is not possible :o
* */
private bool OneTrue()
{
if (!IsLast) return false;
IsLast = false;
return true;
}
I am building a tournament platform. I need to know which is the last match so I can give the players 5 rounds instead of 3. Instead of creating a new column and filling it with false or true, I decided to filter it out. I thought that I could take advantage of LINQ's deferred execution:
I would Select the whole data set from the tournament.
I would then order it by descending and select the first row as the last one. (All the matches were inserted in order, so the biggest id is the last one)
Then filter out which are from the users and which are not.
Possible solutions I think it could work:
- Create a boolean column that will hold "true" value for the last matches.
- Using Specification Pattern (Which I don't know how to apply in this situation. Tried using Expressions and Func but couldn't map them correctly)
- Load all the Ids and select the last one of those Ids. Compare those Ids with the ones that all the users have. Unfortunately this would add an extra roundtrip to the database.
What should I do?
Thanks!
P.S: The OneTrue() method it does what it does, it returns true once, and then it returns false. (Didn't find anything else after a quick Google search)
Edit
For clarification purposes:
The tables show a simulation of the data I currently have. I only need to extract what the current user needs, so I don't need the other 2 rows (which you can see in table #2). Once I select those two rows I exclude the other ones, which could potentially have the last match, but by only selecting this 2 rows I will not know. I'm trying to save any redundancy by trying to query it from the first try. I know that the last match Id is the last of the tournament
So what I was trying to do, is to order them all by descending (because they are in order), and select the last one as the last match.
Could you not just query your subset of data after your return it with linq, like:
var temp = from e in _dbRepositories.TournamentParticipantMatchRepository
where (from f in _dbRepositories.TournamentParticipantMatchRepository
where f.TournamentId == tournamentId)
.Include(x => x.Tournament)
.Include(x => x.Participant1)
.Include(x => x.Participant2)
.Include(x => x.TournamentMatch)
.Select(z => new TournamentParticipantMatchLogicDto
{
IsLastMatch = false, // <==== Problem here,
TournamentParticipantMatch = z
}).Where(x => (x.TournamentParticipantMatch.Participant1Id == participantId || x.TournamentParticipantMatch.Participant2Id == participantId))
.ToListAsync();
int maxResult= temp.Max(t => t.TournamentParticipantMatch.Id);
var update= temp.SingleOrDefault(x => x.TournamentParticipantMatch.Id== maxResult);
if(update!= null)
update.IsLastMatch= true;
This is what I ended up doing. Please if you see any improvements let me know!
public async Task<ParticipantTournament> GetParticipantTournamentByDescending(int tournamentId, int participantId)
{
var lastMatchId = await _dbRepositories.TournamentParticipantMatchRepository
.Where(x => x.TournamentId == tournamentId)
.OrderByDescending(y => y.TournamentMatch.RoundNumber)
.ThenByDescending(y => y.TournamentMatch.Id)
.Select(x => x.Id).FirstOrDefaultAsync();
var response = await _dbRepositories.TournamentParticipantMatchRepository
.Where(x => x.TournamentId == tournamentId)
.Where(x => (x.Participant1Id == participantId || x.Participant2Id == participantId))
.Include(x => x.Tournament)
.Include(x => x.Participant1)
.Include(x => x.Participant2)
.Include(x => x.TournamentMatch)
.ToListAsync();
var logic = response.Select(z=> new TournamentParticipantMatchLogicDto
{
IsLastMatch = z.Id == lastMatchId,
TournamentParticipantMatch = z
})
;
return new ParticipantTournament
{
ParticipantMatches = logic,
};
}
Related
Currently I am doing a keyword search on the Plates table (Name column) but also have a Search (searching on SearchTerm column) table which contains Plat Id's that I also want to search and return the corresponding platforms.
The code below works but I'd like to simplify the logic using an .Include statement if possible although I'm not quite sure how. Any help would be greatly appreciated.
if (!string.IsNullOrEmpty(request.Keyword))
{
var searchTermPlateIds = await _db.Search
.Where(x=> x.SearchTerm.ToLower().Contains(request.Keyword.Trim().ToLower()))
.Select(x => x.PlatformId)
.ToListAsync(ct);
var plateFromPlateIds = await _db.Plate
.OrderBy(x => x.Name)
.Where(x => searchTermPlateIds.Contains(x.Id) && x.Status != PlateStatus.Disabled)
.ToListAsync(ct);
plates = await _db.Plates
.OrderBy(x => x.Name)
.Where(x => !string.IsNullOrEmpty(request.Keyword.Trim()) && x.Name.ToLower().Contains(request.Keyword.Trim().ToLower()) && x.Status != PlateStatus.Disabled)
.ToListAsync(ct);
plates = plates.Union(platesFromPlateIds).ToList();
}
Remember simple thing, Include ONLY for loading related data, not for filtering.
What we can do here - optimize query, to make only one request to database, instead of three.
var query = _db.Plates
.Where(x => x.Status != PlateStatus.Disabled);
if (!string.IsNullOrEmpty(request.Keyword))
{
// do not materialize Ids
var searchTermPlateIds = _db.Search
.Where(x => x.SearchTerm.ToLower().Contains(request.Keyword.Trim().ToLower()))
.Select(x => x.PlatformId);
// queryable will be combined into one query
query = query
.Where(x => searchTermPlateIds.Contains(x.Id);
}
// final materialization, here you can add Includes if needed.
var plates = await query
.OrderBy(x => x.Name)
.ToListAsync(ct);
My models structure looks like this:
UserProgresses:dbset
LessonProgresses:List // The lesson progress a user has in one course
Lesson:Lesson // The general Lesson class.
Materials:List // A list of lesson materials
When I execute this query:
var progresses = context.UserProgresses
.Include(x => x.LessonProgresses.Select(y => y.Lesson.Materials))
.SingleOrDefault(x => x.Id == progressId);
This is the result I get after executing that query:
foreach (var lessonProgress in progress.LessonProgress)
{
lessonProgress.Lesson // Works
lessonProgress.Lesson.Materials // NULL
}
The interesting thing here is that when I insert the row below inside the loop and on the first line the Materials list gets populated.
context.Lessons.Include(x => x.Material)
.SingleOrDefault(x => x.Id == lesson.Lesson.Id);
I also checked the tables and the data is OK. I suspect that something is wrong with the Include statement.
Thanks.
Try to use a string to specify the relationships
var progresses = context.UserProgresses
.Include("LessonProgresses.Lesson.Materials")
.SingleOrDefault(x => x.Id == progressId);
MSDN documentation
Or try this
var progresses = context.UserProgresses
.Include(u => u.LessonProgress.Select(l => l.Lesson).Select(m => m.Materials))
.SingleOrDefault(x => x.Id == progressId);
So the problem was that the LessonProgress was untracked and was added way back in the callstack. The Lesson was set using:
lessonProgress.Lesson = context.Lessons.SingleOrDefault(x => x.Id == lessonId);
Without including Materials. After doing a Include there, everything was all good.
I'm trying to figure out the BRE NRules and got some examples working but having a hard time to match a collection.
IEnumerable<Order> orders = null;
When()
.Match<IEnumerable<Order>>(o => o.Where(c => c.Cancelled).Count() >= 3)
.Collect<Order>(() => orders, o => o.Cancelled);
Then()
.Do(ctx => orders.ToList().ForEach(o => o.DoSomething()));
Basically what I want is if there are 3 orders cancelled then do some action. But I can't seem get a match on a collection, single variables do work.
The program:
var order3 = new Order(123458, customer, 2, 20.0);
var order4 = new Order(123459, customer, 1, 10.0);
var order5 = new Order(123460, customer, 1, 11.0);
order3.Cancelled = true;
order4.Cancelled = true;
order5.Cancelled = true;
session.Insert(order3);
session.Insert(order4);
session.Insert(order5);
session.Fire();
What am I doing wrong here?
With the 0.3.1 version of NRules, the following will activate the rule when you collected 3 or more canceled orders:
IEnumerable<Order> orders = null;
When()
.Collect<Order>(() => orders, o => o.Cancelled)
.Where(x => x.Count() >= 3);
Then()
.Do(ctx => orders.ToList().ForEach(o => o.DoSomething()));
Update:
For posterity, starting with version 0.4.x the right syntax is to use reactive LINQ. Matching a collection will look like this:
IEnumerable<Order> orders = null;
When()
.Query(() => orders, q => q
.Match<Order>(o => o.Cancelled)
.Collect()
.Where(x => x.Count() >= 3));
Then()
.Do(ctx => DoSomething(orders));
In your example, it should be pretty straightforward
IEnumerable<Order> orders = null;
When()
.Collect<Order>(() => orders, o => o.Cancelled == true);
Then()
.Do(ctx => orders.ToList().ForEach(o => o.DoSomething()));
I think the important part is the o.Cancelled alone without the == true. I know this sound wack, but somehow the property evaluation alone that is not an expression (i.e. just the property) is not well supported in NRules.
I ran into this problem and when I added the == true everything fell into place.
How to join Multiple Collection based on some expression like
IEnumerable<RawMsp> rawMsps = null;
IEnumerable<AsmMasterView> asmMasterViews = null;
IEnumerable<AsmInvestor> asmInvestors = null;
When()
.Match<AsmInvestor>(() => rawMsps)
.Match<AsmInvestor>(() => asmInvestor, i => i.InvestorId.ToString() == rawMsp.INVESTOR_CODE)
.Match<AsmMasterView>(() => asmMasterView, x => x.ChildAssumptionHistId == asmInvestor.AssumptionHistId);
Match is applicable individual object , Not sure apply equals of Enumerable Objects
I'm working on a report right now that runs great with our on-premises DB (just refreshed from PROD). However, when I deploy the site to Azure, I get a SQL Timeout during its execution. If I point my development instance at the SQL Azure instance, I get a timeout as well.
Goal: To output a list of customers that have had an activity created during the search range, and when that customer is found, get some other information about that customer regarding policies, etc. I've removed some of the properties below for brevity (as best I can)...
UPDATE
After lots of trial and error, I can get the entire query to run fairly consistently within 1000MS so long as this block of code is not executed.
CurrentStatus = a.Activities
.Where(b => b.ActivityType.IsReportable)
.OrderByDescending(b => b.DueDateTime)
.Select(b => b.Status.Name)
.FirstOrDefault(),
With this code in place, things begin to go haywire. I think this Where clause is a big part of it: .Where(b => b.ActivityType.IsReportable). What is the best way to grab the status name?
EXISTING CODE
Any thoughts as to why SQL Azure would timeout whereas on-premises would turn this around in less than 100MS?
return db.Customers
.Where(a => a.Activities.Where(
b => b.CreatedDateTime >= search.BeginDateCreated
&& b.CreatedDateTime <= search.EndDateCreated).Count() > 0)
.Where(a => a.CustomerGroup.Any(d => d.GroupId== search.GroupId))
.Select(a => new CustomCustomerReport
{
CustomerId = a.Id,
Manager = a.Manager.Name,
Customer = a.FirstName + " " + a.LastName,
ContactSource= a.ContactSource!= null ? a.ContactSource.Name : "Unknown",
ContactDate = a.DateCreated,
NewSale = a.Sales
.Where(p => p.Employee.IsActive)
.OrderByDescending(p => p.DateCreated)
.Select(p => new PolicyViewModel
{
//MISC PROPERTIES
}).FirstOrDefault(),
ExistingSale = a.Sales
.Where(p => p.CancellationDate == null || p.CancellationDate <= myDate)
.Where(p => p.SaleDate < myDate)
.OrderByDescending(p => p.DateCreated)
.Select(p => new SalesViewModel
{
//MISC PROPERTIES
}).FirstOrDefault(),
CurrentStatus = a.Activities
.Where(b => b.ActivityType.IsReportable)
.OrderByDescending(b => b.DueDateTime)
.Select(b => b.Disposition.Name)
.FirstOrDefault(),
CustomerGroup = a.CustomerGroup
.Where(cd => cd.GroupId == search.GroupId)
.Select(cd => new GroupViewModel
{
//MISC PROPERTIES
}).FirstOrDefault()
}).ToList();
I cannot give you a definite answer but I would recommend approaching the problem by:
Run SQL profiler locally when this code is executed and see what SQL is generated and run. Look at the query execution plan for each query and look for table scans and other slow operations. Add indexes as needed.
Check your lambdas for things that cannot be easily translated into SQL. You might be pulling the contents of a table into memory and running lambdas on the results, which will be very slow. Change your lambdas or consider writing raw SQL.
Is the Azure database the same as your local database? If not, pull the data locally so your local system is indicative.
Remove sections (i.e. CustomerGroup then CurrentDisposition then ExistingSale then NewSale) and see if there is a significant performance improvement after removing the last section. Focus on the last removed section.
Looking at the line itself:
You use ".Count() > 0" on line 4. Use ".Any()" instead, since the former goes through every row in the database to get you an accurate count when you just want to know if at least one row satisfies the requirements.
Ensure fields referenced in where clauses have indexes, such as IsReportable.
Short answer: use memory.
Long answer:
Because of either bad maintenance plans or limited hardware, running this query in one big lump is what's causing it to fail on Azure. Even if that weren't the case, because of all the navigation properties you're using, this query would generate a staggering number of joins. The answer here is to break it down in smaller pieces that Azure can run. I'm going to try to rewrite your query into multiple smaller, easier to digest queries that use the memory of your .NET application. Please bear with me as I make (more or less) educated guesses about your business logic/db schema and rewrite the query accordingly. Sorry for using the query form of LINQ but I find things such as join and group by are more readable in that form.
var activityFilterCustomerIds = db.Activities
.Where(a =>
a.CreatedDateTime >= search.BeginDateCreated &&
a.CreatedDateTime <= search.EndDateCreated)
.Select(a => a.CustomerId)
.Distinct()
.ToList();
var groupFilterCustomerIds = db.CustomerGroup
.Where(g => g.GroupId = search.GroupId)
.Select(g => g.CustomerId)
.Distinct()
.ToList();
var customers = db.Customers
.AsNoTracking()
.Where(c =>
activityFilterCustomerIds.Contains(c.Id) &&
groupFilterCustomerIds.Contains(c.Id))
.ToList();
var customerIds = customers.Select(x => x.Id).ToList();
var newSales =
(from s in db.Sales
where customerIds.Contains(s.CustomerId)
&& s.Employee.IsActive
group s by s.CustomerId into grouped
select new
{
CustomerId = grouped.Key,
Sale = grouped
.OrderByDescending(x => x.DateCreated)
.Select(new PolicyViewModel
{
// properties
})
.FirstOrDefault()
}).ToList();
var existingSales =
(from s in db.Sales
where customerIds.Contains(s.CustomerId)
&& (s.CancellationDate == null || s.CancellationDate <= myDate)
&& s.SaleDate < myDate
group s by s.CustomerId into grouped
select new
{
CustomerId = grouped.Key,
Sale = grouped
.OrderByDescending(x => x.DateCreated)
.Select(new SalesViewModel
{
// properties
})
.FirstOrDefault()
}).ToList();
var currentStatuses =
(from a in db.Activities.AsNoTracking()
where customerIds.Contains(a.CustomerId)
&& a.ActivityType.IsReportable
group a by a.CustomerId into grouped
select new
{
CustomerId = grouped.Key,
Status = grouped
.OrderByDescending(x => x.DueDateTime)
.Select(x => x.Disposition.Name)
.FirstOrDefault()
}).ToList();
var customerGroups =
(from cg in db.CustomerGroups
where cg.GroupId == search.GroupId
group cg by cg.CustomerId into grouped
select new
{
CustomerId = grouped.Key,
Group = grouped
.Select(x =>
new GroupViewModel
{
// ...
})
.FirstOrDefault()
}).ToList();
return customers
.Select(c =>
new CustomCustomerReport
{
// ... simple props
// ...
// ...
NewSale = newSales
.Where(s => s.CustomerId == c.Id)
.Select(x => x.Sale)
.FirstOrDefault(),
ExistingSale = existingSales
.Where(s => s.CustomerId == c.Id)
.Select(x => x.Sale)
.FirstOrDefault(),
CurrentStatus = currentStatuses
.Where(s => s.CustomerId == c.Id)
.Select(x => x.Status)
.FirstOrDefault(),
CustomerGroup = customerGroups
.Where(s => s.CustomerId == c.Id)
.Select(x => x.Group)
.FirstOrDefault(),
})
.ToList();
Hard to suggest anything without seeing actual table definitions, espectially the indexes and foreign keys on Activities entity.
As far I understand Activity (CustomerId, ActivityTypeId, DueDateTime, DispositionId). If this is standard warehousing table (DateTime, ClientId, Activity), I'd suggest the following:
If number of Activities is reasonably small, then force the use of CONTAINS by
var activities = db.Activities.Where( x => x.IsReportable ).ToList();
...
.Where( b => activities.Contains(b.Activity) )
You can even help the optimiser by specifying that you want ActivityId.
Indexes on Activitiy entity should be up to date. For this particular query I suggest (CustomerId, ActivityId, DueDateTime DESC)
precache Disposition table, my crystal ball tells me that it's dictionary table.
For similar task to avoid constantly hitting Activity table I made another small table (CustomerId, LastActivity, LastVAlue) and updated it as the status changed.
I have a seemingly simple task that I am having far more trouble than I care to admit doing. I have a hierarchical table that I need to query and display the results grouped by the parent with associated children.
My current LINQ query:
var quests = Questions.Include(q => q.Question2)
.Include(q => q.Sections)
.Include(q => q.QuestionType)
.Include(q => q.AnswerOptions)
.Where(sq => sq.Sections.Any(s => s.SectionId == sectionId))
.OrderBy(q=> q.QuestionId).ThenBy(q => q.ParentQuestionId);
This produces a result set of:
What I want to produce is:
My Question is simply, how can I get the desired results using Lambda syntax.
Update based on Servys' comment.
First line is to make sure all related questions are grouped together.
Second line is to make sure parent question is first.
Third line is to order properly
.OrderBy(q => q.ParentQuestionId == null ? q.QuestionId : q.ParentQuestionId)
.ThenBy(q => q.ParentQuestionId == null ? 0 : 1)
.ThenBy(q => q.DisplayOrder);
So it seems what you're really trying to create here is a tree based structure in which, at the top level, you have all questions with no parent, and then as "child" nodes all questions that have that as a parent.
var questions = GetAllQuestions();//here is where you can put your includes, etc.
var query = questions.Where(q => q.ParentQuestionId != null)
.GroupBy(q => q.ParentQuestionId)
.Select(group => new
{
Parent = questions.First(q => q.QuestionId == group.Key),
Children = group.OrderBy(q => q.DisplayOrder),
})
.OrderBy(group => group.Parent.DisplayOrder);
.OrderBy(x => (x.ParentQuestionId==null?x.QuestionId:x.ParentQuestionId.Value));