c# - LINQ 中 FirstOrDefault() 和 FirstOrDefault<string>() 的区别

标签 c# linq

FirstOrDefault() and FirstOrDefault<string>() 有什么区别?在 LINQ 中?

有人能给我一些简单的解释吗?

TypedDataSet ds= codeComponent.GetAllCode("test");
string status = (from s in ds.Codes
                 where s.Name == "hello"
                 select s.Remarks).FirstOrDefault();


TypedDataSet ds= codeComponent.GetAllCode("test");
string status = (from s in ds.Codes
                 where s.Name == "hello"
                 select s.Remarks).FirstOrDefault<string>();

最佳答案

没有语义差异;编译器能够从集合的类型中推断出泛型参数。只是让我们的生活更简单的语法糖。

请注意,这并非特定于 LINQ;该规范定义了何时可以推断出通用参数以及何时不能推断出它们。

关于c# - LINQ 中 FirstOrDefault() 和 FirstOrDefault<string>() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9405980/

相关文章:

C# 循环/迭代对象以获取具有复杂属性类型的属性值

c# - 如何停止 Xamarin.UITest 在测试之间删除数据?

c# - 这个函数的返回类型应该是什么?

c# - 使用内部节点解析 xml

c# - 为什么我的结果类型为 List<IEnumerable<T>>

c# - 外发光圆角 TextBox

C# NET - 在不透明级别设置的窗体上绘制不透明画笔

c# - 这种类型的数据具有内置行为,不能以这种方式添加到模型中 : System. Int64

c# - Linq 结果 Int 错误 : Specified cast is not valid

C# Linq 字符串与 indexOf 比较