Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I missing something about LINQ?

Tags:

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 using them for other things.

I mean, I could see the benefit of using SQL-like statements on things other than databases. But if I wanted to write SQL, well, why not just write SQL and keep it out of C#? What am I missing here?

like image 283
Jason Baker Avatar asked Aug 21 '08 21:08

Jason Baker


People also ask

What is missing LINQ?

This means that you can use LINQ to query data sources such as ADO.NET DataSets or SQL Server tables and views. But LINQ can query a lot more. XML also “has structure”. LINQ allows queries against any XML data source including an XML file or an XML string in memory. Objects also have structure.

Is LINQ still relevant?

Yes, it is a major part of The Official Microsoft ASP.NET Site MVC model. Absolutley. EFCore and Recular Dotnet core fully embrace using Linq in all it's forms, not just for database operations but for anything that can be represented in a collection.

Is LINQ important in C#?

Users can query XML documents, relational databases and in-memory collections by querying using LINQ. The languages that support LINQ capabilities are VB , C#, etc. The data sources are connected to the language by the LINQ-enabled data sources which includes the LINQ flavors like LINQ to Objects, LINQ to ADO.

Is LINQ easy to learn?

LINQ is composable in nature and it can be used to solve complex problems into a series of short, comprehensible queries that are easy to debug. LINQ is declarative, it is very easy to understand and maintain.


1 Answers

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.

like image 168
FlySwat Avatar answered Nov 07 '22 07:11

FlySwat