Balazs Szekeres
Balazs Szekeres

Reputation: 105

How do I return a element using LINQ query from an ObservableCollection?

I have an ObservableCollection:

public ObservableCollection<NurDeviceWatcherInfo> KnownDevices = new ObservableCollection<NurDeviceWatcherInfo>();

NurDeviceWatcherInfo has these string properties: Address, ConnState, Info, Name, Spec, SpecStr, Tag, TransportType.

What I am trying to use is:

NurDeviceWatcherInfo device = from NurDeviceWatcherInfo in KnownDevices 
                                where NurDeviceWatcherInfo.Name.Contains("NUR") 
                                select new NurDeviceWatcherInfo();

For which I receive the following error:

Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'NurApiDotNet.UWP.NurDeviceWatcherInfo'. An explicit conversion exists (are you missing a cast?)

What would be the correct syntax?

Upvotes: 2

Views: 263

Answers (1)

Balazs Szekeres
Balazs Szekeres

Reputation: 105

I solved it using :

device = KnownDevices.Where(x => x.Name.Contains("NUR")).FirstOrDefault();

Upvotes: 1

Related Questions