Pregunta

This doesn't work (0 items):

var cfg = from n in context.Source<Win32_NetworkAdapterConfiguration>()
    where n.Description == theDescription
    select n;

But this does:

var cfg = new List<Win32_NetworkAdapterConfiguration>();

var x = from n in context.Source<Win32_NetworkAdapterConfiguration>()
    select n;

foreach (var i in x) { if (i.Description == theDescription) cfg.Add(i); }

Why ?

¿Fue útil?

Solución

I presume you are using the Linq To Wmi project at http://linq2wmi.codeplex.com. If so, then the published version only supports constants and not variables in query expressions. (Your query would end up generating something like ... where Description = 'theDescription' ... which is unlikely to return any results.)

But if you go to http://linq2wmi.codeplex.com/SourceControl/list/patches you will see that someone has submitted a patch to resolve this issue. It was submitted in 2009 but is never accepted.

There have been a few minor changes to the published source since then, but it should be fairly trivial to merge them.

Otros consejos

I imagine this has something to do with the binding time of the data with LINQ and the implementation used in WMILinq. You could contact the author of that library.

Or you can try this:

void Main()
{
    string filter = "WAN Miniport (SSTP)";
    string qry = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE description = '" + filter + "'";
    ManagementObjectSearcher r = new ManagementObjectSearcher(qry);
    var items = r.Get();
}

Comparing strings with LINQ is case sensitive...

if n.Description equals "hello" and theDescription equals "HELLO", n.Description == theDescription will be false.

try something like

where string.Equals(n.Description, theDescription, StringComparison.OrdinalIgnoreCase)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top