I have a large join statement in linq that is taking far too long to execute (over 2 minutes). I made it the way it was to try and see if the database was set up correctly and I was getting the data properly, but now the project is almost complete and that kind of delay is unacceptable.
The problem is rather obviously the String.Join statements but I don't know how to do it another way, so my question is how can I modify the link statement to get the same information, but in a quicker way. I had thought that maybe it would work better to just join the data and store it somewhere else on the creation of one of the entities that needed joining but if possible it would be better to just have this statement be better.
It is possible to have no components for a ProjectQueryElement with a function of "b" or "m" etc.
var dc = datacontext;
var resultSet = (
from r in dc.requests
select new ProjectQueryElement {
bKey = String.Join( "|", dc.comps
.Where(
x => x.reRequestId == r.requestId && x.function == "b"
).Select( x => x.idNumber )
),
mKey = String.Join( "|", dc.comps
.Where(
x => x.reRequestId == r.requestId && x.function == "m"
).Select( x => x.idNumber )
),
oKey = String.Join( "|", dc.comps
.Where(
x => x.reRequestId == r.requestId && x.function == "o"
).Select( x => x.idNumber )
),
pKey = String.Join( "|", dc.comps
.Where(
x => x.reRequestId == r.requestId && x.function == "p"
).Select( x => x.idNumber )
),
rKey = String.Join( "|", dc.comps
.Where(
x => x.reRequestId == r.requestId && x.function == "r"
).Select( x => x.idNumber )
)
}
);
The resultSet will then be passed to a jqgrid as the rows for the grid.
randdc.comps, given that you only care about elements ofdc.compswhere thereRequestIdisr.requestId... but have you looked at what the SQL looks like?StringBuilderand build the string with help of them.string.Join? I'd challenge any and all assumptions. If the problem is the join, what if you just didn't do it, and instead left the strings in arrays?String.Joins and that is for sure the problem. Without them it still takes longer than I'd like, but much faster than with the joins. I hadn't considered leaving them as arrays because that hasn't been done anywhere else in the project, this resultset gets passed to ajqgridimmediately after and I'm not possitive the effects of an array going into it.