Am I missing something about LINQ?

后端 未结 6 559
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 01:23

I seem to be missing something about LINQ. To me, it looks like it\'s taking some of the elements of SQL that I like the least and moving them into the C# language and usin

6条回答
  •  死守一世寂寞
    2020-12-31 01:58

    LINQ is not about SQL. LINQ is about being apply functional programming paradigmns on objects.

    LINQ to SQL is an ORM built ontop of the LINQ foundation, but LINQ is much more. I don't use LINQ to SQL, yet I use LINQ all the time.

    Take the task of finding the intersection of two lists:

    Before LINQ, this tasks requires writing a nested foreach that iterates the small list once for every item in the big list O(N*M), and takes about 10 lines of code.

    foreach (int number in list1)
    {
        foreach (int number2 in list2)
        {
            if (number2 == number)
            {
                returnList.add(number2);
            }
        }
    }
    

    Using LINQ, it does the same thing in one line of code:

    var results = list1.Intersect(list2);
    

    You'll notice that doesn't look like LINQ, yet it is. You don't need to use the expression syntax if you don't want to.

提交回复
热议问题