I'm trying to select one field last record in filtered database (this is different than last inserted record). I tried with following code in controller but instead of field value, i'm getting "true" or "false", depending on if there's results after filtering or not.

List<Pozicije> poz = new List<Pozicije>();
poz = db.Pozicijes.Where(p => p.grupa == grupa)
                    .OrderBy(p => p.sifra_pozicije).ToList();
string pos = poz.Select(p => p.sifra_pozicije.Contains(s)).LastOrDefault().ToString();

can someone point me how to get value i need instead?

有帮助吗?

解决方案

Try this instead. I've combined both parts of your query into one.

 var pos =
     Convert.ToString(db.Pozicijes.Where(p => p.grupa == grupa
                                              && p.sifra_pozicije.Contains(s))
                                  .OrderByDescending(p => p.sifra_pozicije)
                                  .Select(p => p.sifra_pozicije)
                                  .FirstOrDefault());

If it doesn't work, you may need to tell us what types s and sifra_pozicije are.

其他提示

LastOrDefault is not supported with LINQ to Entities/LINQ TO SQL. You need to do OrderByDescending and then get First record. Like:

string pos = db.Pozicijes.Where(p => p.grupa == grupa &&  p.sifra_pozicije.Contains(s)))
            .OrderByDescending(p=> p.sifra_pozicije)
            .Select(r=> r.sifra_pozicije)
            .First();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top