hallo, I am new in mvc and linq. I've 2 table in relation many to many: 1)employe 2)device
Selecting an employe I'd like to see his devices:
-Marco -Franco Selecting Marco should appear the devices associated to him.
My code (not work):
Nel controller:
public ActionResult VediDevice(int id)
{ Impiegato impi = new Impiegato(); List<Device> ListDevice = new List<Device>(); using (DatabaseMVCCOntrolsEntities db = new DatabaseMVCCOntrolsEntities())
{
ListDevice= db.Devices.Where(u => db.ImpiegatiDevice.Any(m => m.impiegatiID == id)).ToList(); //<---???
{ Impiegato impi = new Impiegato(); List<Device> ListDevice = new List<Device>(); using (DatabaseMVCCOntrolsEntities db = new DatabaseMVCCOntrolsEntities())
{
ListDevice= db.Devices.Where(u => db.ImpiegatiDevice.Any(m => m.impiegatiID == id)).ToList(); //<---???
//or
ListDevice= db.Devices.ToList().Where(u => u.item.Any(m => m.impiegatiID == id)).ToList(); //<--??
}
return View(ListDevice);
}
}
return View(ListDevice);
}
In device.cs I added:
List<ImpiegatiDevice> item{get;set;}
VediDevice.cshtml:
@model List<MVCControlToolkit.Models.Device>
@{
ViewBag.Title = "VediDevice";
}
VediDeviceh2>
<table class="table table-striped table-condensed">
NOME
th>
tr>
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.nome)
td>
tr>
}
table>
Not work.
What should I do?
Thank you
Ale
12 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsForums
Top experts
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Dhara Patel108 reputationPosted Nov 28, 2019, 12:11 AM EST
shozo lojacoono17 reputationPosted Nov 28, 2019, 2:13 AM EST
Errore CS0266 Non è possibile convertire in modo implicito il tipo 'System.Linq.IQueryable
shozo lojacoono17 reputationPosted Nov 28, 2019, 3:16 AM EST
Dhara Patel108 reputationPosted Nov 28, 2019, 4:28 AM EST
shozo lojacoono17 reputationPosted Nov 28, 2019, 4:57 AM EST
Ashutosh GuptaRank #805 · 2.9K reputationPosted Nov 28, 2019, 5:24 AM EST
shozo lojacoono17 reputationPosted Nov 28, 2019, 5:38 AM EST
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Device()
{
this.ImpiegatiDevice = new HashSet
}
public int Id { get; set; }
public string nome { get; set; }
public string descrizione { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection
}
Ashutosh GuptaRank #805 · 2.9K reputationPosted Nov 28, 2019, 5:41 AM EST
shozo lojacoono17 reputationPosted Nov 28, 2019, 6:00 AM EST
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Impiegato()
{
this.ImpiegatiDevice = new HashSet
}
public int Id { get; set; }
public string nome { get; set; }
public string cognome { get; set; }
public string email { get; set; }
public Nullable
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection
}
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Device()
{
this.ImpiegatiDevice = new HashSet
}
public int Id { get; set; }
public string nome { get; set; }
public string descrizione { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection
}
{
public int Id { get; set; }
public Nullable
public Nullable
public virtual Device Device { get; set; }
public virtual Impiegato Impiegati { get; set; }
public List
}
Ashutosh GuptaRank #805 · 2.9K reputationPosted Nov 28, 2019, 6:14 AM EST
shozo lojacoono17 reputationPosted Nov 28, 2019, 9:20 PM EST
select Devices.nome from Devices where Devices.id IN(select DeviceID from ImpiegatoDevices where ImpiegatoDevices.impiegatiID=id);
shozo lojacoono17 reputationPosted Nov 28, 2019, 11:20 PM EST