English 中文(简体)
如果我使用像NHibernate那样的ORM,那么为什么我需要LINQ?
原标题:Why do I need LINQ if I use NHibernate-like ORMs?

我正在阅读这一SO问题,但我仍然不清楚一件具体的事情。

如果我使用NHibernate,为什么我需要LINQ?

我认为,如果我知道NHibernate也包括LINQ支持,问题就变得更加严重。

LINQ to NHibernate?

WTF!

最佳回答

确实,你不需要Linq,你总是可以不这样做,但你可能想要这样做。

林克提供了一种办法,可以表达根据可以查询的数据进行操作,然后我们能够根据数据状况开展其他行动。 它有意撰写,以便尽可能地分析这些数据是否属于专题收集、XML、数据库等。 归根结底,它总是以某种内层物体为作业,有一些手段在内层和最终来源之间转换,尽管有些约束比其他约束更进一步,将一些行动推向另一层。 例如,电话.Count() 最终可检索Count 财产,通过收集进行,并保持标记,向数据库发送查询,或者可能的话。

投管处提供了一种办法,可相互反映元件和数据库浏览量,而变数则反映在对面的变化中。

这完全属于上文“某些转换手段”的范围。 因此,Linq2SQL、EF和Linq2NHibern均履行林业局的作用和Linq供应商的作用。

Considering that Linq can work on collections you d have to be pretty perverse to create an ORM that couldn t support Linq at all (you d have to design your collections to not implement IEnumerable<T> and hence not work with foreach). More directly supporting it though means you can offer better support. At the very least it should make for more efficient queries. For example if an ORM gave us a means to get a Users object that reflected all rows in a users table, then we would always be able to do:

  int uID = (from u in Users where u.Username == "Alice" select u.ID).FirstOrDefault();

若不直接支持林克,则将<代码>Users 执行<编码>IQueryable<User>,即:

SELECT * FROM Users

后续行动:

while(dataReader.Read())
  yield return ConstructUser(dataReader);

后续行动:

foreach(var user in Users)
  if(user.Username == "Alice")
    return user.ID;
return 0;

实际上,这比这差一点。 在直接支助下,制作的KQry将:

SELECT TOP 1 id FROM Users WHERE username =  Alice 

然后C#就等于

return dataReader.Read() ? dataReader.GetInt32(0) : 0;

很显然,林克供应商越多地支持林克公司,如何更好地运作。

Linq is an in-language feature of C# and VB.NET and can also be used by any .NET language though not always with that same in-language syntax. As such, ever .NET developer should know it, and every C# and VB.NET developer should particularly know it (or they don t know C# or VB.NET) and that s the group NHibernate is designed to be used by, so they can depend on not needing to explain a whole bunch of operations by just implementing them the Linq way. Not supporting it in a .NET library that represents queryable data should be considered a lack of completeness at best; the whole point of an ORM is to make manipulating a database as close to non-DB related operations in the programming language in use, as possible. In .NET that means Linq supprt.

问题回答

LINQ is a query language. It allows you to express queries in a way that is not tied in to your persistence layer.

您可能想到ORM

利用LINQ来命名这两者,是造成你等不幸的混乱。

nHibernate, EF, LINQ2XML are all LINQ holder - 所有这些都允许你利用LINQ syntax获得数据来源m>。

首先,光是所有准则都不是管理办公室。 无论物体来自何方,都质问物体。

因此,你可以与Nhibernate使用LINQ,这是完全有意义的。

我认为,你误解了LINQ至LINQ至

Common sense?

人力厅如NHibernate和汇编者采用综合办法来表达在很多情况下完全使用的询问,两者之间存在差别。

Or: Usage of LINQ (not LINQ to SQL etc. - the langauge, which is what you talk about though I am not sure you meant what you said) means you dont have to deal with Nhibernate special query syntax.

或者:任何使用LINQ的NOT,无论是否为NHibernate,都没有很好的解释。

您不问need,但你可能认为这有用。 正如其他人所说的那样,Linq不像Linq tokou一样。 在我工作的地方,我们撰写了自己的问询,以检索数据,但很常见的是,为了满足具体需要,利用林克操纵数据。 例如,你可能有一个数据检索方法,使你能够检索Dave所拥有的所有狗:

new DogOwnerDal().GetForOwner(id);

如果你只关心Dave s daschunds,满足一个具体需要,而业绩是很多问题,那么你就可以利用Linq过滤所有Dave s dogs的回复,以达到你所需要的具体数据:

new DogOwnerDal().GetForOwner(id).Where(d => d.Breed == DogBreeds.Daschund);

如果业绩至关重要,你可能想写出一种具体的数据存取方法,由所有人取回狗和培育,但在许多情况下,为创建新的数据存取方法所花费的努力并没有提高效率,足以值得做。

In your example, you might want to use NHibernate to retrieve a lump of data, and then use Linq to break that data into lots of individual subsets for some form of processing. It might well be cheaper to get the data once and use Linq to split it up, instead of repeatedly interrogating the database for different mixtures of the same data.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签