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
  • 17,975
  • 14
  • 74
  • 124
senzacionale
  • 19,912
  • 67
  • 198
  • 310

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
  • 37,915
  • 11
  • 80
  • 100
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,174
  • 1
  • 22
  • 33
1

Use the IndexOf method:

kriteriji.IndexOf(doc);
Otiel
  • 17,975
  • 14
  • 74
  • 124
1

Try this:

var position = kriteriji.IndexOf(doc);
Abdul Munim
  • 18,235
  • 7
  • 50
  • 60
0

You can find out the index with:

kriteriji.IndexOf(doc.First());
Fischermaen
  • 11,812
  • 2
  • 37
  • 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