实体框架中select和first之间的区别 [英] difference between select and first in entity framework

查看:64
本文介绍了实体框架中select和first之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请定义之间的区别



Student stud = context.Students.First(i => i.Roll_No == 1);



学生stud = context.Students.select();



我们如何使用select ????

please define the difference between

Student stud = context.Students.First(i => i.Roll_No == 1);
and
Student stud = context.Students.select();

and how can we use select????

推荐答案

我相信你的意思是Select(Func)vs First(Func)签名,因为没有Select的参数免费签名。

好吧,首先,First迭代IEnumerable并在满足条件时停止。这意味着,无论集合的大小如何,在第一次出现Func返回true时,它将停止并返回正在迭代的项目。

选择(Func),另一方面,返回另一个集合(IEnumerable),它可能与原始集合的底层类型相同,也可能不同。这是因为Select作为转换器(将项目投射到另一个项目中)。这也意味着它将遍历所有集合,它将返回第二个未引用的IEnumerable。当你需要转换一个集合或者你需要将一个集合的所有项目的属性投射到另一个集合时,这非常有用,例如:



var lista = new List< Tuple< int,string>>();

IEnumerable< int> ids = lista.Select(a => a.Item1);



参考: http://msdn.microsoft.com/en-us/library/bb548891.aspx [ ^ ]



http://msdn.microsoft.com/en-us/library/bb535050.aspx [ ^ ]
I believe you mean the Select(Func) vs First(Func) signature, as there is no parameter free signature of Select.
Well, first of all, First iterates the IEnumerable and stops as it''s condition is satisfied. This means that, no matter the size of the collection, on the first occurrence that "Func" returns true, it''ll stop and return the item that was being iterated.
Select(Func), on the other hand, returns another collection (IEnumerable) wich may or may not be the same underlying type as the original one. That''s because Select works as a "conversor" (projects an item into another item). This also means it''ll iterate through all the collection, and it''ll return a second and unreferenced IEnumerable. That''s really useful when you need to convert a collection or when u need to project a property of all the items of a collection into another one, for example:

var lista = new List<Tuple<int, string>>();
IEnumerable<int> ids = lista.Select(a => a.Item1);

Reference: http://msdn.microsoft.com/en-us/library/bb548891.aspx[^]
and
http://msdn.microsoft.com/en-us/library/bb535050.aspx[^]


这篇关于实体框架中select和first之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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