UPDATE m
SET m.Score = s.AScore + '-' + s.BScore
FROM #Matches m
INNER JOIN Scores s ON m.MatchId = s.MatchId
AND s.InfoTypeId = (
CASE
WHEN m.SportId = 1 AND (m.StatusId >= 13 AND m.StatusId <= 17) THEN 10
WHEN m.SportId = 1 AND m.StatusId = 20 THEN 24
WHEN m.SportId = 1 AND m.StatusId = 21 THEN 23
WHEN m.SportId = 1 AND m.StatusId = 18 THEN 8
ELSE 5
END
)
I'm having two lists in C# one is Matches and 2nd is Scores, and I want to get the result from those list like the result this query will return. Means I want to update "Score" property of "Matches" list like it is being updated in SQL query.
Any Help Please.
Matches.ForEach(m => m.Score = (Scores.Where(ms => ms.MatchId == m.MatchId
&& ms.ScoreInfoTypeId == ((m.SportId == 1 && m.StatusId >= 13 && m.StatusId <= 17) ? 10
: (m.SportId == 1 && m.StatusId == 20) ? 24
: (m.SportId == 1 && m.StatusId == 21) ? 23
: (m.SportId == 1 && m.StatusId == 18) ? 8
: 5)).Select(ms => ms.AScore + "-" + ms.BScore).FirstOrDefault()));
I have tried, but I think its too expensive. It is taking too much time. Is there any optimized way please.
Try this example in LinqPad. You can use query syntax to join the 2 lists and iterate over the result to set match scores. I used a dictionary to simplify that switch case.
void Main()
{
var matches = new[]{
new Match{MatchId=1,SportId=1,StatusId=13,Score=""},
new Match{MatchId=2,SportId=1,StatusId=18,Score=""},
new Match{MatchId=3,SportId=2,StatusId=24,Score=""},
};
var scores = new[]{
new{MatchId=1,AScore="10",BScore="0",InfoTypeId=10},
new{MatchId=2,AScore="20",BScore="0",InfoTypeId=8},
new{MatchId=3,AScore="30",BScore="0",InfoTypeId=5},
};
var dict = new Dictionary<int,int>{[13]=10,[14]=10,[15]=10,[16]=10,[17]=10,[20]=24,[21]=23,[18]=8};
var data = (from m in matches
join s in scores on m.MatchId equals s.MatchId
where s.InfoTypeId == ((m.SportId == 1 && dict.ContainsKey(m.StatusId))? dict[m.StatusId] : 5)
select new {m,s}
).ToList();
data.ForEach(o =>
{
o.m.Score = o.s.AScore + "-" + o.s.BScore;
});
matches.Dump();
}
class Match{public int MatchId; public int SportId; public int StatusId; public string Score;}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.