Kaan Eroğlu
Kaan Eroğlu

Reputation: 63

LINQ query filters not returning correct number of results

I have a search form with many filter fields. First I get all fields to a var like:

var obj = (from kb in db.kimlik_bilgileri
            join dbilgi in db.dil_bilgisi on kb.id equals dbilgi.KimlikId
            join okbil in db.okul_bilgileri on kb.id equals okbil.KimlikId
            join kurbil in db.kurum_bilgileri on kb.id equals kurbil.KimlikId
            join ens in db.enstitu on kurbil.kurum_id equals ens.id
            join kadr in db.kadro_unvan on kurbil.kadro_id equals kadr.id
            where kb.Aktifmi == true select new PSearchModel
                    {
                        DilBilgisi = dbilgi,
                        // Diller =  dil,
                        OkulBilgileri = okbil,
                        KadroUnvan = kadr,
                        Enstitu = ens,
                        KimlikBilgileri = kb,
                        KurumBilgileri = kurbil
                    }).ToList();

After this, I tried to start filtering by fields like:

if (!yabanciDil.IsNullOrWhiteSpace())
{

    obj = (from o in obj
        where o.DilBilgisi.dil_id == Convert.ToInt32(yabanciDil)
           select new PSearchModel
           {
               DilBilgisi = o.DilBilgisi,
               KadroUnvan = o.KadroUnvan,
               KimlikBilgileri = o.KimlikBilgileri,
               OkulBilgileri = o.OkulBilgileri,
               // Diller = o.Diller,
               Enstitu = o.Enstitu,
               KurumBilgileri = o.KurumBilgileri

           }).ToList();
}

First obj count with 2k data which I expected, but second filter returned count 0. I have 4 or 5 filters also to apply. What did I do wrong or anyone with this condition can describe this.

Upvotes: 2

Views: 216

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062770

Usually, the approach for filtering data like this is with cumulative query composition; for example:

IQueryable<PSearchModel> query =
    from kb in db.kimlik_bilgileri
    join dbilgi in db.dil_bilgisi on kb.id equals dbilgi.KimlikId
    join okbil in db.okul_bilgileri on kb.id equals okbil.KimlikId
    join kurbil in db.kurum_bilgileri on kb.id equals kurbil.KimlikId
    join ens in db.enstitu on kurbil.kurum_id equals ens.id
    join kadr in db.kadro_unvan on kurbil.kadro_id equals kadr.id
    where kb.Aktifmi == true
    select new PSearchModel
    {
        DilBilgisi = dbilgi,
        // Diller =  dil,
        OkulBilgileri = okbil,
        KadroUnvan = kadr,
        Enstitu = ens,
        KimlikBilgileri = kb,
        KurumBilgileri = kurbil
    };

if (!yabanciDil.IsNullOrWhiteSpace())
{
    int dil_di = Convert.ToInt32(yabanciDil);
    query = query.Where(o => o.DilBilgisi.dil_id == dil_di);
}
// ... add other filters here

var list = query.ToList();

This defers all execution to the end, and allows you to inspect dil_di when debugging, to see if it is what you expect. If you still don't get what you expect, then you'll have to investigate what SQL is being issued, to see why your results are different from your expectations.

Upvotes: 1

Lukas Hieronimus Adler
Lukas Hieronimus Adler

Reputation: 1094

You could search in your List with LINQ as well. One possibility is this:

YourListObject.Where(c=>c.Test.Equals("Hallo"))

Also possible to Order or Group by equivalent to this.

Upvotes: 1

Related Questions