2

I have this expression:

var query = from inventar in _context.Inventar
                        join inventarsoba in _context.InventarSoba.Where(x => x.SobaId == request.SobaId)
                        on inventar.InventarId equals inventarsoba.InventarId
                        select new { inventar, inventarsoba.InventarSobaId };

Which basically gets me a list of all "Inventar" records whose ids are also located in "InventarSoba" table. End result is a list of anonymous type, which has a "Inventar" type object and a InventarSobaId integer, all within one element.

I'm basically trying to store all of these inside a class called InventarModel, which has both the InventarSobaId and Inventar fields that I'm trying to send back. However when I run this code:

 List<InventarModel> Lista = new List<InventarModel>();

    if (query.Count() > 0)
    {
        InventarModel obj;
        for (int i = 0; i < query.Count(); i++)
        {
            var obj2 = query.ElementAt(i);

            obj = new InventarModel
            {
                InventarId = obj2.inventar.InventarId,
                Naziv = obj2.inventar.Naziv,
                SobaId = request.SobaId,
                InventarSobaId = obj2.InventarSobaId
            };
            Lista.Add(obj);
        }
    }
    return Lista;

It says that ElementAt(i) is not supported, that it cannot parse the value with ElementAt method, and trying to index it with [] doesn't work at all. Please do note that when I use the "First" function, it works fine with the "obj" variable getting the very first row's data properly, however I can't make it work with indexing.

Simply put I'm trying to take the data from the query and store it all in one class which will have a List and I don't know how to go about it.

Just in case it's needed, here's what the classes Inventar and InventarModel look like:

Inventar

   public partial class Inventar
    {
        public Inventar()
        {
            InventarSoba = new HashSet<InventarSoba>();
        }

        public int InventarId { get; set; }
        public string Naziv { get; set; }

        public virtual ICollection<InventarSoba> InventarSoba { get; set; }
    }

InventarModel

public class InventarModel
{
    public int InventarId { get; set; }
    public string Naziv { get; set; }
    public int SobaId { get; set; }
    public int InventarSobaId { get; set; }
}
WhatAmIDoing
  • 109
  • 1
  • 11
  • 1
    It's not meant to work with indexing. You want `foreach (var obj2 in query) {...}` -- but with a descriptive name for the loop variable instead of `obj2`. – 15ee8f99-57ff-4f92-890c-b56153 Sep 30 '19 at 13:48
  • 1
    Add `.ToList()` to create a list of items. Then you have the complete list and can access elements by index. – Stefan Sep 30 '19 at 13:49
  • in LINQ to SQL it is not translated (https://stackoverflow.com/questions/5147767/why-is-the-query-operator-elementat-is-not-supported-in-linq-to-sql) – vhr Sep 30 '19 at 13:49
  • Thanks, it worked when I just used the foreach statement – WhatAmIDoing Sep 30 '19 at 13:54
  • 2
    You are iterating twice. Why don't you combine the query with the creation of InventarModel so it will iterate once? – Azhar Khorasany Sep 30 '19 at 14:03
  • Use `.ToList()` first and than it will work!!!! – tgralex Sep 30 '19 at 14:12
  • A lot of comments and answers talking about using `ToList()`. Note that there is [a performance impact to that extension method](https://stackoverflow.com/q/15516462/215552) one should keep in mind... – Heretic Monkey Sep 30 '19 at 14:18
  • Also, `Count() > 0` will iterate over your `IQueryable` at least once. Better to use `Any()` instead, which will stop if it finds at least one element in the collection. – Heretic Monkey Sep 30 '19 at 14:22

2 Answers2

1
 List<InventarModel> Lista = 
         (from inventar in _context.Inventar
          join inventarsoba in _context.InventarSoba.Where(x => x.SobaId == request.SobaId)
          on inventar.InventarId equals inventarsoba.InventarId
          select new InventarModel()
          {
             //Assign values here from the inventarsoba and inventar
             InventarId = inventar.InventarId,
             Naziv = inventar.Naziv,
             SobaId = request.SobaId,
             InventarSobaId = inventarsoba.InventarSobaId
          }).ToList();
Canica
  • 2,650
  • 3
  • 18
  • 34
Rony Patel
  • 357
  • 1
  • 2
  • 14
0

Thank you everyone, putting in foreach instead of for did the trick.

However, someone suggested I just do it in one iteration and just add to list at the end, which works like a charm, here's how I did it in the end:

public override List<InventarModel> Get(InventarSearchRequest request)
{

    var query = (from inventar in _context.Inventar
                 join inventarsoba in _context.InventarSoba.Where(x => x.SobaId == request.SobaId)
                 on inventar.InventarId equals inventarsoba.InventarId
                 select new InventarModel()
                 {
                     InventarId = inventar.InventarId,
                     Naziv = inventar.Naziv,
                     SobaId = request.SobaId,
                     InventarSobaId = inventarsoba.InventarSobaId
                 }).ToList();

    return query;
}
WhatAmIDoing
  • 109
  • 1
  • 11