1

kriteriji is type of List<Kriteriji>

var doc = kriteriji.Where(k => k.Ean == txtEan.Text
                     && k.PredmetObravnave == acPredmetObravnave.Text
                     && k.Tse == txtTse.Text
                     && k.DejanskaKolicina == Convert.ToInt32(txtKolicina.Text)
                     && k.KratekNazEnoteMere == acKNEnotaMere.Text
                     && k.OznakaLokacije == acOznakaLokacije.Text
                     && k.OznakaZapore == txtZapora.Text
                     && k.SarzaDob == txtSarzaDobavitelja.Text
                     && k.Sarza == txtSarza.Text
                     && k.DatumVelOd == datumOd
                     && k.DatumVelDo == datumDo).FirstOrDefault();

Now when I get doc how can I know in which position in List<kriteriji> is? I need to now if is in first, second,...

Otiel
  • 18,404
  • 16
  • 78
  • 126
senzacionale
  • 20,448
  • 67
  • 204
  • 316

5 Answers5

3

You can use an overload for select that will take an index and a Kriteriji.

Here is the documentation.

Then you could select an anonymous object with an Index property and a Doc property. If you would use IndexOf this will cause another search throughout the list while you already have that data.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
3

I think you could create a (index , value) keyvaluepaire object at first and then retrive it like

        var doc = kriteriji.Select((value, index) => new { index, value })
            .Where(k => k.value.Ean == txtEan.Text
                 && k.value.PredmetObravnave == acPredmetObravnave.Text
                 && k.value.Tse == txtTse.Text
                 && k.value.DejanskaKolicina == Convert.ToInt32(txtKolicina.Text)
                 && k.value.KratekNazEnoteMere == acKNEnotaMere.Text
                 && k.value.OznakaLokacije == acOznakaLokacije.Text
                 && k.value.OznakaZapore == txtZapora.Text
                 && k.value.SarzaDob == txtSarzaDobavitelja.Text
                 && k.value.Sarza == txtSarza.Text
                 && k.value.DatumVelOd == datumOd
                 && k.value.DatumVelDo == datumDo).FirstOrDefault();

then you could get the index like

Console.WriteLine(doc.index);
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
1

Use the IndexOf method:

kriteriji.IndexOf(doc);
Otiel
  • 18,404
  • 16
  • 78
  • 126
1

Try this:

var position = kriteriji.IndexOf(doc);
Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
0

You can find out the index with:

kriteriji.IndexOf(doc.First());
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
  • 1
    It looks like not doc.First(), but doc - because of it returns doc is an 1 element but not collection – Vitaliy Nov 21 '11 at 08:52