Tacy Nathan Tacy Nathan - 1 month ago 10
C# Question

List<Guid> with LINQ

I have a method that returns a list of Guids. I want the following linq query in it:

var results = (from t in CurrentDataSource.Table1
where t.Manager == userId && t.Profile != null
select t.Profile).ToList();


Why do I get the following error:

Error 4 Cannot implicitly convert type 'System.Collections.Generic.List<System.Guid?>' to 'System.Collections.Generic.List<System.Guid>'

Answer

You're checking if t.Profile is null, and only returning valid Guid's, so an explicit cast should work:

var results = (from t in CurrentDataSource.Table1
           where t.Manager == userId && t.Profile != null
           select (Guid)t.Profile).ToList();