什么是linq以及为什么要使用它? [英] what is linq and why use it ?

查看:258
本文介绍了什么是linq以及为什么要使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供我能够轻松理解的简单答案?

解决方案

LINQ

它代表语言集成查询。我们使用LINQ to Objects,而Objects部分是任何IEnumerable集合。它使您能够独立于数据源查询和操作数据。它就像一种查询语言,可以查询任何数据源和任何转换。它还提供完整的类型安全性和编译时间检查。 



为什么:

 LINQ旨在简化查询数据源。 
.NET Framework包含允许任何人创建可以查询任何数据源的LINQ提供程序的库。
LINQ提供程序,可以轻松查询专用数据源。


Linq是一个通用的'前端',允许您查询类似的数据在数据库上使用SQL的方法。



我说'通用'这只是意味着有不同的linq'引擎'允许它运行在不同的东西上。 br />


例如,Linq to SQL允许你在你的C#程序中编写,Linq引擎将把它(在幕后)翻译成SQL的代码运行,并返回结果。



最重要的是(恕我直言)Linq to Objects - 也就是说,只使用Linq本地语言。



假设您的程序中有动物对象集合,并且用户选择了他们只想看到有4条腿的动物。



你可以去



<前lang =c#> foreach (动物动物动物)
{
if (animal.Legs == 4
{
foundAnimals.Add(animal);
}
}
返回 foundAnimals;





这很好,工作效率很高,但是,需要几行代码。



使用Linq,你可以做同样的事情。 />


 返回 Animals.Where(a = >  a.Legs ==  4 )。ToList(); 





还有更多的东西 - 但这就是基础知识(这就是你要求的!)


你好...

Linq是什么。为什么我们在.net中使用linq [ ^ ]

谢谢你。


provide the simple answer that i can easily understand?

解决方案

LINQ

It stands for Language Integrated Query. We are using LINQ to Objects and the Objects part is any IEnumerable collection.  It enables you to query and manipulate data independently of data soureces. It's like a query language which can query any data source and any transform. It also provides full type safety and compile time checking. 


Why:

LINQ is intended to make it easy to query data sources. 
.NET Framework includes libraries that allow anyone to create a LINQ provider that can query any data source. 
LINQ providers that make it easy to query specialized data sources.


Linq is a generic 'front end' allowing you to query data in a similar way to using SQL on a database.

Whan I say 'generic' this just means there are different linq 'engines' that allow it to run over different things.

For example, there is Linq to SQL which allows you to write in your C# program, code which the Linq engine will translate (behind the scenes) into SQL, run, and return the results.

Best of all (IMHO) is Linq to Objects - that is, just using Linq natively in the language.

Say you have a collection of Animal objects in your program, and the user has selected they only want to see animals with 4 legs.

You could go

foreach (Animal animal in Animals)
{
    if (animal.Legs == 4)
    {
        foundAnimals.Add(animal);
    }
}
return foundAnimals;



Which is fine, and works efficiently, but, well, takes a few lines of code.

With Linq you can do the same thing as

return Animals.Where(a => a.Legs == 4).ToList();



There's loads more to it - but that's that's the basics (which is what you asked for!)


Hi...
what is Linq. why we use linq in .net.[^]
Thank u.


这篇关于什么是linq以及为什么要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆