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 4234550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:22:18+00:00 2026-05-21T02:22:18+00:00

This should be an easy one for Linq guru’s. I wanna retrieve all the

  • 0

This should be an easy one for Linq guru’s.

I wanna retrieve all the physical printers that are available to a user. As far as I can tell there is no property/setting that you can query which tells you whether a printer is a physical of a virtual one. But there’s already a question about that topic : Is there a possibility to differ virtual printer from physical one?

I’m using a reliable-ish way to see if it’s a virtual one, and I check whether the “EnableBIDI” property is set to true.

 ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
 ManagementObjectCollection coll = searcher.Get();
 var installedPhysicalPrinters = new List<string>();


        var scratch = from ...

The result should be that the installedPhysicalPrinters contains a list of installedprinter names.

I got this far

        foreach (ManagementObject printer in coll)
        {

            var validPrinter = (from PropertyData p in printer.Properties
                                from ManagementObject pr in coll
                                where p.Name == "EnableBIDI" && (bool) p.Value == true
                                select p).FirstOrDefault();

            if (validPrinter != null)
            {

                installedPrinters.Add(validPrinter.Value.ToString());

                // this is just to see the properties
                foreach (PropertyData property in printer.Properties)
                {

                    lstMessages.Items.Add(string.Format("{0}: {1}", property.Name, property.Value));


                } 
            }


        }

Obviously this is wrong, and I end up with a list of “true” values in the list, instead of the printer names.

Using nested for eaches it’s easy to solve, but that’s sooo 2004…

  • 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-05-21T02:22:19+00:00Added an answer on May 21, 2026 at 2:22 am

    The issue with combining LINQ and System.Management is that the latter uses older non-generic containers (like ManagementObjectCollection) that you can’t use directly with LINQ because it uses the non-generic IEnumerable interface.

    Luckily you can work around that, as in the following example:

    static void Main(string[] args)
    {
        Func<PropertyData, bool> indicatesPhysical = property => property.Name == "EnableBIDI"; // && (bool) property.Value;
        Func<ManagementBaseObject, bool> isPhysicalPrinter = obj => obj.Properties.OfType<PropertyData>().Any(indicatesPhysical);
    
        var searcher = new ManagementObjectSearcher("Select * from Win32_Printer");
        var collection = searcher.Get();
        var physicalPrinters = collection.OfType<ManagementBaseObject>().Where(isPhysicalPrinter);
    
        foreach (var item in physicalPrinters)
        {
            Console.WriteLine(item.Properties["DeviceID"].Value);
            Console.WriteLine(item.Properties["EnableBIDI"].Value);
        }
    }
    

    You can use the Enumerable.OfType<>() extension method to transform the specified IEnumerable interface into an IEnumerable<ManagementBaaseObject>. That will help in getting those queries along.

    Your example on how to differentiate between physical and virtual printers didn’t work on my end, but it’s irrelevant to what the main problem is that you’re dealing with, namely trying to make code more readable by using LINQ expressions.

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

Sidebar

Related Questions

This should be and easy one for the LINQ gurus out there. I'm doing
Should be an easy one. I thought, from reading this blog post that I
argh, this should be an easy one. How can I set the directory that
This should be easy one, I just haven't found any reference to that.
This should be an easy one. How to write a jquery function that gets
this should be an easy one and I really thought that what I was
This should be an easy one. I have a function that traverses a csv
This should be an easy one to track down...but it isn't proving that way
This should be an easy one, but through all the research I've done on
This should be any easy one for you. I have an accordion that I'd

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.