我正在嘗試在單個表上對具有不同類別(CarreraId)的行進行交叉連接,并計算給定組合的點總和。但是,使用此代碼,如果沒有給定類別的行(假設 CarreraId=6),則查詢回傳 0 個專案。我希望查詢回傳 72 個組合(在此示例中)和 0 用于不存在類別 (CarreraId) 的值。
不知道如何處理這種情況。另外,我想知道是否有辦法使用 Dynamic Linq 來解決這類問題。
任何幫助表示贊賞。
var marcas = new List<Marca>
{
new Marca(1, 1, 0),
new Marca(1, 5, 1),
new Marca(2, 3, 0),
new Marca(2, 8, 5),
new Marca(2, 10, 0),
new Marca(3, 1, 0),
new Marca(4, 6, 5),
new Marca(4, 9, 2),
new Marca(4, 12, 1),
new Marca(5, 7, 1),
new Marca(5, 11, 1),
new Marca(6, 1, 0), // <-- Comment this line to reproduce the problem
new Marca(6, 2, 0) // <-- Comment this line to reproduce the problem
};
var resultado = from marca1 in marcas where marca1.CarreraId == 1 orderby marca1.ParticipanteId ascending
from marca2 in marcas where marca2.CarreraId == 2 orderby marca1.ParticipanteId ascending
from marca3 in marcas where marca3.CarreraId == 3 orderby marca1.ParticipanteId ascending
from marca4 in marcas where marca4.CarreraId == 4 orderby marca1.ParticipanteId ascending
from marca5 in marcas where marca5.CarreraId == 5 orderby marca1.ParticipanteId ascending
from marca6 in marcas where marca6.CarreraId == 6 orderby marca6.ParticipanteId ascending
select new {
Carrera1 = marca1.ParticipanteId,
Carrera2 = marca2.ParticipanteId,
Carrera3 = marca3.ParticipanteId,
Carrera4 = marca4.ParticipanteId,
Carrera5 = marca5.ParticipanteId,
Carrera6 = marca6.ParticipanteId, // Tried to use this to no avail --> Carrera6 = (marca6?.Participante == null ? 0 : marca6.Participante),
Puntos = marca1.Puntos marca2.Puntos marca3.Puntos marca4.Puntos marca5.Puntos marca6.Puntos // Tried this as well --> marca6?.Puntos
};
resultado.ToList().ForEach(r => Console.WriteLine(r));
record Marca(int? CarreraId, int? ParticipanteId, int? Puntos);
uj5u.com熱心網友回復:
類似于下面的內容-您可能只需要 CarreraId == 6 上的 .defaultifempty
var resultado2 = from marca1 in marcas.Where(x => x.CarreraId == 1).DefaultIfEmpty()
orderby marca1.ParticipanteId ascending
from marca2 in marcas.Where(x => x.CarreraId == 2).DefaultIfEmpty()
orderby marca1.ParticipanteId ascending
from marca3 in marcas.Where(x => x.CarreraId == 3).DefaultIfEmpty()
orderby marca1.ParticipanteId ascending
from marca4 in marcas.Where(x => x.CarreraId == 4).DefaultIfEmpty()
orderby marca1.ParticipanteId ascending
from marca5 in marcas.Where(x => x.CarreraId == 5).DefaultIfEmpty()
orderby marca1.ParticipanteId ascending
from marca6 in marcas.Where(x => x.CarreraId == 6).DefaultIfEmpty()
orderby marca6 == null ? 0 : marca6.ParticipanteId ascending
select new
{
Carrera1 = marca1.ParticipanteId,
Carrera2 = marca2.ParticipanteId,
Carrera3 = marca3.ParticipanteId,
Carrera4 = marca4.ParticipanteId,
Carrera5 = marca5.ParticipanteId,
Carrera6 = (marca6 == null ? 0 : marca6.ParticipanteId),
Puntos = marca1.Puntos marca2.Puntos marca3.Puntos marca4.Puntos marca5.Puntos (marca6 == null ? 0 : marca6.Puntos) //
};
resultado2.ToList().ForEach(r => Console.WriteLine(r));
uj5u.com熱心網友回復:
使用 Lambda 樣式的聚合函式解決
var marcas = new List<Marca>
{
new Marca(1, 1, 0),
new Marca(1, 5, 1),
new Marca(2, 3, 0),
new Marca(2, 8, 5),
new Marca(2, 10, 0),
new Marca(3, 1, 0),
new Marca(4, 6, 5),
new Marca(4, 9, 2),
new Marca(4, 12, 1),
new Marca(5, 7, 1),
new Marca(5, 11, 1),
new Marca(6, 1, 0),
new Marca(6, 2, 0)
};
var resultado = marcas.GroupBy(m => m.CarreraId);
var resultado1 = resultado.Skip(1)
.Aggregate(resultado.First()
.Select(i => new List<Marca>() { i }),
(previous, next) => previous
.SelectMany(m => next.Select(e => new List<Marca>(m) { e })));
record Marca(int? CarreraId, int? ParticipanteId, int? Puntos);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/454038.html
標籤:林克
