简体   繁体   English

实体框架6:筛选表格

[英]Entity Framework 6: filter a table

I', approaching to EF6. 我正在接近EF6。 I have understood that, if I want a single record, I can use the First() method (usable also like a "get", I think). 我知道,如果我想要一条记录,则可以使用First()方法(我认为它也可以像“ get”一样使用)。

But what if I want a collection of records from my table? 但是,如果我要从表中收集记录怎么办? I mean, if the result of query can have a cardinality from 0 to N... what have I to do? 我的意思是,如果查询结果的基数可以从0到N ...我该怎么办?

Silly Example: 愚蠢的例子:

Name table: SomeTable 名称表:SomeTable

ID    NAME    SOMEATTR
1      A       YELLOW
2      B        RED
3      C       YELLOW
4      D       YELLOW
5      E        BLUE

... ...

How can I get all the records where someAttr is "yellow"? 如何获得someAttr为“黄色”的所有记录?

Once you have your Linq query... 一旦您有Linq查询...

var query = (from ... in ... where ... select ...);

Or, for your specific example: 或者,对于您的特定示例:

var query = (from r in dbContext.MyTable where (r.someAttr == "YELLOW") select r);

You can either just iterate over it using foreach which will cause it to be executed once and then yield the results ... 您可以使用foreach对其进行迭代,这将导致它执行一次,然后产生结果...

foreach (var r in query)
{
}

... or call ToList() ( http://msdn.microsoft.com/en-us/library/vstudio/bb342261(v=vs.100).aspx ) if you want to acquire a list that can, later, be passed around, manipulated or examined many times. ...或致电ToList()http://msdn.microsoft.com/zh-cn/library/vstudio/bb342261 ToList() .aspx ),如果您想获取一个列表,以后可以被多次传递,操纵或检查。 (This list is essentially disconnected from the server after it is returned.) (返回后,此列表实际上已与服务器断开连接。)

var list = query.ToList()

... or use it to construct a collection like a HashSet<T> ( http://msdn.microsoft.com/en-us/library/bb301504(v=vs.110).aspx ) ...或使用它来构建类似HashSet<T>的集合( http://msdn.microsoft.com/en-us/library/bb301504(v=vs.110).aspx

var set = new HashSet<T>(query)

Any of these will cause it to be executed on the server, only once, and process the results. 这些中的任何一个都将使其仅在服务器上执行一次,并处理结果。

After all, the query itself is IEnumerable . 毕竟,查询本身是IEnumerable It is also IQueryable so you could manipulate it further and then enumerate the results, like... 它也是IQueryable因此您可以进一步操作它, 然后枚举结果,例如...

var sortedList = query.OrderBy(...).ToList()

using linq & C# 使用linq和C#

private readonly myEntities db = new myEntities()
public list<SomeTable> MyRecords()
{
 return res = db.SomeTables.where(o => o.SOMEATTR == "YELLOW").ToList()
}

If you have a SOMEATTR class that maps to SOMEATTR table, you can query it: 如果您有一个映射到SOMEATTR表的SOMEATTR类,则可以查询它:

var context = new YourDbContext();
var results = context.SomeTables.Where(x=>x.SOMEATTR == "Yellow").ToList();

//then you can iterate all results
foreach(var item in results)
{
    Console.WriteLine(item.ID + " " + item.NAME + " " + item.SOMEATTR);
}

The result will be: 结果将是:

1      A       YELLOW
3      C       YELLOW
4      D       YELLOW

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM