Sto usando linq per unire più tabelle e scrivere query complesse. Qui quando avrò '0' come qualsiasi parametro, ad esempio CategoryId
, GameId
, LimitVariantId
, significa che l'utente ha selezionato "Tutti" dall'interfaccia.Operatore ternario in LINQ dove clausola
mia query SQL, quando passerò parametro valori superiori a '0' è:
select * from dbo.GameCombinations gc
inner join dbo.StakeBuyInByStakeCategories sbsc
on sbsc.StakeBuyInByStakeCategoryId = gc.StakeBuyInByStakeCategoryId
inner join dbo.GameTables gt
on gc.GameCombinationId = gt.GameCombinationId
where gc.CurrencyId=1 and gc.GameTypeId=2
and sbsc.StakeBuyInId=gt.BuyIn
and gc.CategoryId=4
and gc.GameId=7
and gc.LimitVariantId=23
and gc.StakeCategoryId in (3,5,6)
Quando passerò CategoryId come 0
poi mia query SQL saranno:
select * from dbo.GameCombinations gc
inner join dbo.StakeBuyInByStakeCategories sbsc
on sbsc.StakeBuyInByStakeCategoryId = gc.StakeBuyInByStakeCategoryId
inner join dbo.GameTables gt
on gc.GameCombinationId = gt.GameCombinationId
where gc.CurrencyId=1 and gc.GameTypeId=2
and sbsc.StakeBuyInId=gt.BuyIn
--and gc.CategoryId=4
and gc.GameId=7
and gc.LimitVariantId=23
and gc.StakeCategoryId in (3,5,6)
Così Non ho bisogno di includere i campi in cui clausola. Per questo ho scritto il seguente LINQ:
ProviderDB db = new ProviderDB();
try
{
IQueryable<dynamic> query;
if (StakeCategoryIdsByStakeBuyIn != null)
{
query = (from gc in db.GameCombinations.Where(x => x.CurrencyId == CurrencyId && x.GameTypeId == GameTypeId
&& CategoryId <= 0 ? true : x.CategoryId == CategoryId
&& GameId <= 0 ? true : x.GameId == GameId
&& LimitVariantId <= 0 ? true : x.LimitVariantId == LimitVariantId
&& StakeCategoryIdsByStakeBuyIn.Contains(x.StakeCategoryId)
)
join sbsc in db.StakeBuyInByStakeCategories
on gc.StakeBuyInByStakeCategoryId equals sbsc.StakeBuyInByStakeCategoryId
join gt in db.GameTables
on gc.GameCombinationId equals gt.GameCombinationId
join gx in db.Games
on gc.GameId equals gx.GameId into joined
from gx in joined.DefaultIfEmpty()
where gt.BuyIn == sbsc.StakeBuyInId
select new
{
GameTableId = gt.GameTableId,
Description = gt.Description,
BuyIn = gt.BuyIn,
Table = gx.GameName,
MaxAllowPlayer = gt.MaxAllowPlayer
}).Distinct();
}
else
{
query = (from gc in db.GameCombinations.Where(x => x.CurrencyId == CurrencyId && x.GameTypeId == GameTypeId
&& CategoryId == 0 ? true : x.CategoryId == CategoryId
&& GameId == 0 ? true : x.GameId == GameId
&& LimitVariantId == 0 ? true : x.LimitVariantId == LimitVariantId
&& StakeCategoryIdsByStakeBuyIn == null
)
join sbsc in db.StakeBuyInByStakeCategories
on gc.StakeBuyInByStakeCategoryId equals sbsc.StakeBuyInByStakeCategoryId
join gt in db.GameTables
on gc.GameCombinationId equals gt.GameCombinationId
join sb in db.StakeBuyIns
on gt.BuyIn equals sb.StakeBuyInId
join gx in db.Games
on gc.GameId equals gx.GameId into joined
from gx in joined.DefaultIfEmpty()
where gt.BuyIn == sbsc.StakeBuyInId
select new
{
GameTableId = gt.GameTableId,
Description = gt.Description,
BuyIn = sb.StakeBuyInValue,
Table = gx.GameName,
MaxAllowPlayer = gt.MaxAllowPlayer
}).Distinct();
}
Ma questo restituirà tutti i campi dal mio database. Quindi qualcuno può aiutarmi a scrivere queste query in LINQ con condizioni ternarie che restituiranno i record dei miei campi filtrati?
Potete per favore elaborare il vostro requisito? –
è un operatore ternario che rende questo più leggibile? – Jodrell