سؤال

I am using linq to sql for my project. But getting error after I am converting the result of ExecuteQuery() using the .ToList() extension:

var logentries = DB.ExecuteQuery<keyValueList>(string.Format(query));
keyValueList kv1 = logentries.ToList();// -->Error in this line as: 

Error:

Cannot implicitly convert type 'System.Collections.Generic.List' to 'DataLib.keyValueList'

هل كانت مفيدة؟

المحلول

ToList returns a List<T> where T is keyValueList. But i think you want a single object, then either use First/FirstOrdefault or Singler/ SingleOrDefault:

keyValueList kv1 = logentries.FirstOrDefault();

The diference is, the ...OrDefault methods return null if the input sequnce is empty whereas the First/Single throw an exeption. Furthermore Single throws an exception if there is more than one item(useful if that is impossible and should throw, for example because you are filtering by a unique property).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top