This is what I’m trying to do, but it doesn’t work:
HardwareType hwt = new HardwareType { HType = "PC" };
IEnumerable<Hardware> Pcs = db.Hardware.Where(h => h.HardwareType.Contains(hwt));
ViewBag.Pcs = Pcs.ToString();
So how do I convert my IEnumerable to a string (or other primitive data type) so the compiler won’t give me an error when I try to use it in my Razor?
@foreach (var item in ViewBag.Pcs) {
<li><a href="#" class="btn"><i class="icon-hdd"></i> @item.HType</a></li>
}
Hardware class:
public class Hardware
{
public int Id { get; set; }
public virtual ICollection<DeviceType> Type { get; set; }
public string AssetTagId { get; set; }
public virtual ICollection<Manufacturer> Manufacturer { get; set; }
[Required]
[StringLength(50)]
public string ServiceTagId { get; set; }
[Required]
[StringLength(50)]
public string SerialNumber { get; set; }
[Required]
[StringLength(75)]
public string ProductNumber { get; set; }
// [Required]
[StringLength(20)]
public string PurchaseDate { get; set; }
[StringLength(20)]
public string WarrantyExpiration { get; set; }
[Required]
[StringLength(20)]
public string WarrantyType { get; set; }
public virtual ICollection<Location> Location { get; set; }
public virtual ICollection<HardwareType> HardwareType { get; set; }
[Required]
[StringLength(2000)]
public string Notes { get; set; }
public string POATag { get; set; }
}
HardwareType class:
public class HardwareType
{
public int Id { get; set; }
[Required]
[StringLength(128)]
public string HType { get; set; }
public virtual ICollection<Hardware> Hardware { get; set; }
}
It looks to me as if the problem is that you are converting it to a string, here:
Why are you doing that? It looks like you want to iterate over the individual items within the sequence within your view. Either use:
or
if you want to force the query to be materialized.
EDIT: Looking at the error again, I suspect it’s actually the
Whereclause that’s the problem. I suspect you really want:The problem is trying to use your
hwtvalue as input to the query…