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; }
}