Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:54:22+00:00 2026-06-15T19:54:22+00:00

This is what I’m trying to do, but it doesn’t work: HardwareType hwt =

  • 0

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; }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:54:23+00:00Added an answer on June 15, 2026 at 7:54 pm

    It looks to me as if the problem is that you are converting it to a string, here:

    ViewBag.Pcs = Pcs.ToString();
    

    Why are you doing that? It looks like you want to iterate over the individual items within the sequence within your view. Either use:

    ViewBag.Pcs = Pcs;
    

    or

    ViewBag.Pcs = Pcs.ToList();
    

    if you want to force the query to be materialized.

    EDIT: Looking at the error again, I suspect it’s actually the Where clause that’s the problem. I suspect you really want:

    var pcs = db.Hardware.Where(h => h.HardwareType.Any(hwt => hwt.HType == "PC"));
    ViewBag.Pcs = pcs.ToList(); // Materialize the query
    

    The problem is trying to use your hwt value as input to the query…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This current code doesn't work. $loaded_data.find('img').match(/\.(jpg|png|gif)/i).first().attr('src') It will allow img with the ext of
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This is a bit of a long shot, but if anyone can figure it
This is kinda oddball, but I was poking around with the GNU assembler today
This might seem like a stupid question I admit. But I'm in a small
This has been posted, but I've tried lot of solutions found on SO and
This is a pretty stale question but frankly I'm yet to find it answered
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
This is my code: Stopwatch timer = new Stopwatch(); timer.Start(); while (timer.ElapsedMilliseconds < 3000)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.