2013-05-20 22 views
8

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?

+0

Potete per favore elaborare il vostro requisito? –

+2

è un operatore ternario che rende questo più leggibile? – Jodrell

risposta

4

con LINQ to SQL (con LINQ in generale) è possibile aggiungere le condizioni Where a livello di codice, ad esempio:

var query = db.GameCombinations.Where(x => x.CurrencyId == CurrencyId && x.GameTypeId == GameTypeId); 

if (CategoryId > 0) 
{ 
    query = query.Where(x => x.CategoryId == CategoryId); 
} 

e così via.

Inoltre, è meglio usare "inferenza di tipo" (usando la parola chiave var) invece di dynamic, non sarà possibile ottenere intellisense con dynamic

[Edit] La LINQ to SQL provider di raggrupperà tutte le condizioni Where durante la conversione in SQL

+0

Grazie è utile per me. – KomalJariwala

1

è ancora possibile eseguire la query in SQL, provare qualcosa di simile:

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 (0 = categoryParameter OR gc.CategoryId=categoryParameter) //Pass 0 to take all categories 
and gc.GameId=7 
and gc.LimitVariantId=23 
and gc.StakeCategoryId in (3,5,6) 

EDIT:

fare lo stesso per gli altri parametri:

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 || x.CategoryId == CategoryId) 
              && (GameId == 0 || x.GameId == GameId) 
              && (LimitVariantId == 0 || 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(); 
        } 
      } 
+1

Grazie. ma ho bisogno di una query in linq. – KomalJariwala

+1

Utilizza lo stesso principio della query precedente. Prova a sostituire 'CategoryId == 0? true: x.CategoryId == CategoryId' with '(CategoryId == 0 || x.CategoryId == CategoryId)'. Anche gli altri parametri – noobob

+0

@noobob, +1, '0' potrebbero non essere il giusto valore" altro ", potrebbe essere" null ", ma questo principio funziona perfettamente. –

0

L'esempio è molto complicato, ma in sostanza si sta utilizzando linq per creare una query.

var someIQueryableInProgress = ... ; 

if (categoryId != 0) 
{ 
    someIQueryableInProgress = someIQueryableInProgress 
     .Where(s => s.categoryId = categoryId) 
} 

Quindi valutare quando tutte le condizioni sono applicate e lasciare che il provider faccia il lavoro per voi.