LINQ is merely the addition of some functional programming concepts to C#/VB. Hence, yes, most things tend to get much easier. C# 2.0 actually had some of this -- see the List methods, for instance. (Although, anonymous method syntax in C# 2.0 was too verbose.)
If you have a list (i.e. List<Palette> palettes) that contains objects which contains another list (i.e. Palette.Colors) and want to flatten all those sub-lists into one:
String.Join("\n", @"some VB6 code
that I could refactor automatically
if FindAndReplace were a bit more powerfully
And I don't want to refactor by hand".Split('\n').Trim().Select(line =>
{
if(line.Contains("FindAndReplace") && !line.StartsWith("//"))
{
return line.Split(" ").Last();
}
else
{
return String.Join("_", line.Split(" ").Take(3));
}
});
String.Join("\n", @"some VB6 code
that I could refactor automatically
if FindAndReplace were a bit more powerfully
And I don't want to refactor by hand".Split('\n').Trim().Select(line =>
{
if(line.Contains("FindAndReplace") && !line.StartsWith("//"))
{
return line.Split(" ").Last();
}
else
{
return String.Join("_", line.Split(" ").Take(3));
}
});
You get the idea. Linq lets me apply the full power of C# to text transformation. Typically I use it when I have code in one language that I want to extract and manipulate in a complex manner, I throw the text alone in LinqPad and do a "find-replace" on quotation marks, replacing them with double quotation marks, then I surround it by @"..." and get to work. I can usually do massive code transformations in 30 seconds or so.
void Main()
{
// Demonstrates dynamic ordering
var SortExpression = "Total"; // choose ProductName or Total
var sortAscending = true; // choose true for ascending, false for descending
// the query to sort
var data = (from pd in Products
join od in OrderDetails on pd.ProductID equals od.ProductID into DetailsByProduct
select new { ProductName = pd.ProductName, Total = DetailsByProduct.Count()}).ToList();
// the data source you can use for data binding
var ds= (sortAscending)
? data.OrderBy(x => x.GetType().GetProperty(SortExpression).GetValue(x, null))
: data.OrderByDescending(x => x.GetType().GetProperty(SortExpression).GetValue(x, null));
ds.Dump();
}
My favorite is the following Linq example for dynamically sorting a SQL table from the Northwind database:
void Main()
{
// Demonstrates dynamic ordering
var SortExpression = "Total"; // choose ProductName or Total
var sortAscending = true; // choose true for ascending, false for descending
// the query to sort
var data = (from pd in Products
join od in OrderDetails on pd.ProductID equals od.ProductID into DetailsByProduct
select new { ProductName = pd.ProductName, Total = DetailsByProduct.Count()}).ToList();
// the data source you can use for data binding
var ds= (sortAscending)
? data.OrderBy(x => x.GetType().GetProperty(SortExpression).GetValue(x, null))
: data.OrderByDescending(x => x.GetType().GetProperty(SortExpression).GetValue(x, null));
ds.Dump();
}
It allows you to dynamically sort by simply specifying a field in the variable SortExpression and a sort order in the variable sortAscending.
By using .Take(x) and .Skip(y) you can as well implement paging easily.
发布评论
评论(9)
过滤掉列表中的空项目。
创建一个字典,其中键是属性的值,值是该属性在列表中出现的次数。
Filter out null items in a list.
Create a dictionary where the key is the value of a property, and the value is the number of times that property appears in the list.
LINQ 只是在 C#/VB 上添加了一些函数式编程概念。 因此,是的,大多数事情都会变得容易得多。 C# 2.0 实际上有一些这样的功能——例如,请参阅 List 方法。 (尽管 C# 2.0 中的匿名方法语法过于冗长。)
下面是一个小例子:
LINQ is merely the addition of some functional programming concepts to C#/VB. Hence, yes, most things tend to get much easier. C# 2.0 actually had some of this -- see the List methods, for instance. (Although, anonymous method syntax in C# 2.0 was too verbose.)
Here's one little example:
示例 1
返回包含本地网络中所有可用 SQL Server 实例名称的列表
示例 2
为新文件生成未使用的名称
Example 1
Returns list with names of all available instances of SQL Server within the local network
Example 2
Generates not used name for new file
我有两个我喜欢的非常荒谬但优雅的例子,
尽管它们被封装为查询运算符,因此您可以重用它们,例如用于二进制解析
I have two nicely absurd but elegant examples that I love
Albeit these are encapsulated as query operators so you can reuse them, e.g. for binary parsing
如果您有一个分隔的
Name=Value
字符串,例如"ID=2;Name=James;Age=32;"
并且您希望将其快速转换为字典, 您可以使用:If you have a delimited
Name=Value
string, such as"ID=2;Name=James;Age=32;"
and you want to turn this into a dictionary quickly, you can use:如果您有一个列表(即
List调色板
),其中包含包含另一个列表(即Palette.Colors
)的对象,并且希望将所有这些子列表展平为一:If you have a list (i.e.
List<Palette> palettes
) that contains objects which contains another list (i.e.Palette.Colors
) and want to flatten all those sub-lists into one:当我移植代码时,我喜欢在文本上使用 LINQ:
例如:
您明白了。 Linq 让我能够将 C# 的全部功能应用于文本转换。 通常,当我有一种语言的代码,我想以复杂的方式提取和操作时,我会使用它,我将文本单独放入 LinqPad 中,并对引号进行“查找-替换”,用双引号替换它们,然后我用
@"..."
包围它并开始工作。 我通常可以在 30 秒左右完成大量代码转换。I like to use LINQ on text when I'm porting code:
For example:
You get the idea. Linq lets me apply the full power of C# to text transformation. Typically I use it when I have code in one language that I want to extract and manipulate in a complex manner, I throw the text alone in LinqPad and do a "find-replace" on quotation marks, replacing them with double quotation marks, then I surround it by
@"..."
and get to work. I can usually do massive code transformations in 30 seconds or so.我最喜欢的是以下 Linq 示例,用于从 Northwind 数据库:
它允许您通过简单地在变量
SortExpression
中指定字段并在变量中指定排序顺序来动态排序升序排序。
通过使用
.Take(x)
和.Skip(y)
,您还可以轻松地实现分页。My favorite is the following Linq example for dynamically sorting a SQL table from the Northwind database:
It allows you to dynamically sort by simply specifying a field in the variable
SortExpression
and a sort order in the variablesortAscending
.By using
.Take(x)
and.Skip(y)
you can as well implement paging easily.让我开始了,太棒了!
Got me started and its awesome!