Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较LINQ之前的代码_C#_Linq - Fatal编程技术网

C# 比较LINQ之前的代码

C# 比较LINQ之前的代码,c#,linq,C#,Linq,我试图了解LINQ出现之前c代码的样子 我已经找了好几个星期了,结果一无所获。我理解LINQ是如何工作的,但我说你有一个对象列表,但你只是试图找到一小部分。在LINQ之前你是怎么做到的 LINQ示例请原谅我的语法错误,我仍在学习: list<employee> newlist = new List<employee> {john, smith, 30} newlist.add{jane, smith, 28} newlist.add{greg, lane

我试图了解LINQ出现之前c代码的样子

我已经找了好几个星期了,结果一无所获。我理解LINQ是如何工作的,但我说你有一个对象列表,但你只是试图找到一小部分。在LINQ之前你是怎么做到的

LINQ示例请原谅我的语法错误,我仍在学习:

 list<employee> newlist = new List<employee> {john, smith, 30}
    newlist.add{jane, smith, 28}
    newlist.add{greg, lane, 24}

var last
from name in newlist
where name.last.equals("smith")
select name

foreach(var name in last)
{
Console.WriteLine(last);
}

如何按姓氏对员工姓名进行排序和定位,并将其显示出来?

非常简单。您正在循环浏览列表中的所有项目,这意味着复制对其他列表的引用您正在查找的项目:

var smiths = new List<Persons>();
// get all Smiths and print them
foreach(var item in newlist)
{
    if(item.last == "smith")
        smiths.Add(item);
}

foreach(var item in smiths)
{
    Console.WriteLine(item);
}

很简单。您正在循环浏览列表中的所有项目,这意味着复制对其他列表的引用您正在查找的项目:

var smiths = new List<Persons>();
// get all Smiths and print them
foreach(var item in newlist)
{
    if(item.last == "smith")
        smiths.Add(item);
}

foreach(var item in smiths)
{
    Console.WriteLine(item);
}

只是传统的方式。循环并过滤

var smiths = new List<string>();
foreach (var employee in newlist)
{
   if(employee.Last == "smith")
   {
       smiths.Add(employee);
   }
}

return smiths;
另一种排序方法是创建一个实现IComparer的类,并将其传递给sort方法

newlist.Sort(new LastNameComparer());

class LastNameComparer: IComparer<Employee>
{
   public int Compare(Employee e1, Employee e2)
   {
       // your comparison logic here that compares two employees
       return String.Compare(e1.Last, e2.Last);
   }
}

看看所有这些代码,LINQ非常省时:

只是传统的方式。循环并过滤

var smiths = new List<string>();
foreach (var employee in newlist)
{
   if(employee.Last == "smith")
   {
       smiths.Add(employee);
   }
}

return smiths;
另一种排序方法是创建一个实现IComparer的类,并将其传递给sort方法

newlist.Sort(new LastNameComparer());

class LastNameComparer: IComparer<Employee>
{
   public int Compare(Employee e1, Employee e2)
   {
       // your comparison logic here that compares two employees
       return String.Compare(e1.Last, e2.Last);
   }
}

看看所有这些代码,LINQ非常节省时间:

它的代码行数实际上是相同的,只是更多的花括号

这里有一个翻译:

List<employee> newList = new List<employee> 
{
    new employee {First = john, Last = smith, Age = 30},
    new employee {First = jane, Last = smith, Age = 28},
    new employee {First = greg, Last = lane, Age = 24},
}

// Original code:                 // Pre-Linq translation:
var last                          // Becomes: var last = new List<employee>();
from name in newList              // Becomes: foreach (var name in newList) {
where name.Last.Equals("smith")   // Becomes: if (name.Last.Equals("smith") {
select name                       // Becomes: last.Add(name) } }

// Pre-Linq code:
var last = new List<employee>();
foreach (var name in newList) 
{
    if (name.Last.Equals("smith") 
    {
        last.Add(name) 
    } 
}

它的代码行数实际上是相同的,只是更多的花括号

这里有一个翻译:

List<employee> newList = new List<employee> 
{
    new employee {First = john, Last = smith, Age = 30},
    new employee {First = jane, Last = smith, Age = 28},
    new employee {First = greg, Last = lane, Age = 24},
}

// Original code:                 // Pre-Linq translation:
var last                          // Becomes: var last = new List<employee>();
from name in newList              // Becomes: foreach (var name in newList) {
where name.Last.Equals("smith")   // Becomes: if (name.Last.Equals("smith") {
select name                       // Becomes: last.Add(name) } }

// Pre-Linq code:
var last = new List<employee>();
foreach (var name in newList) 
{
    if (name.Last.Equals("smith") 
    {
        last.Add(name) 
    } 
}

for/foreach+if..你不是在要求排序,而是在要求筛选。虽然大多数答案都给出了这个时代的典型代码,但LINQ继续说。理解收益率对于理解LINQ的许多运算符是至关重要的。@ TMBBROGDGET——为什么你说理解收益率是理解许多LINQ运算符的关键?@ Enigmativity收益率是IQueLaby的懒惰迭代的基础,这是LINQ的全部内容;生成一个查询,然后您可以懒洋洋地迭代。for/foreach+if..您不是要求排序,而是要求筛选。虽然大多数答案都给出了该时代的典型代码,但确实进行了LINQ。理解收益率对于理解LINQ的许多运算符是至关重要的。@ TMBBROGDGET——为什么你说理解收益率是理解许多LINQ运算符的关键?@ Enigmativity收益率是IQueLaby的懒惰迭代的基础,这是LINQ的全部内容;生成一个查询,然后进行惰性迭代。