我正在使用 linq 连接多个表并编写复杂的查询。在这里,当我将“0”作为任何参数时,即CategoryId
, GameId
, LimitVariantId
,这意味着用户已从界面中选择“全部”。
我的 Sql 查询,当我将传递大于 '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)
当我传递 CategoryId 时,0
我的 Sql 查询将是:
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)
所以我不需要在 where 子句中包含这些字段。为此,我编写了以下 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();
}
但这将返回我数据库中的所有字段。那么任何人都可以帮助我用三元条件在 LINQ 中编写这些查询,以返回我过滤字段的记录吗?