Linq to实体以动态/严格类型过滤实体 [英] Linq to Entities Filtering an Entity Dynamic/Strongly Typed
问题描述
我正在将Winforms网格绑定到实体. (出于某种原因,我将不赘述,它必须绑定到实体,而不是查询的结果).代码如下:
I am binding a Winforms Grid to an entity. (For reasons I won't go into here it must be bound to the entity, not the result a query) The code is as follows:
grid.DataSource = myEntities.entityName.Where("it.field = " & field)
它可以工作,但是显然不是强类型的.是否可以使用强类型表示法定义实体的Where子句?
It works, but it obviously isn't strongly typed. Is there a way to define the Where clause of an entity using a strongly typed notation?
推荐答案
您是否尝试过使用lambda表达式?
Have you tried to use a lambda expression?
grid.DataSource = myEntities.Customers.Where(c => c.Name == "Bob");
或在VB中:
grid.DataSource = myEntities.Customers.Where(Function(c) c.Name = "Bob")
如果它必须是动态的,那么您可能要考虑构建自定义的表达式树.有关表达式树基础的教程,请参见此博客
If it has to be dynamic then you might want to look at building a custom Expression Tree. For a tutorial on the basics of Expression Trees see this blog http://blogs.msdn.com/charlie/archive/2008/01/31/expression-tree-basics.aspx
此博客显示了一个很好的排序示例. http://weblogs.asp. net/davidfowler/archive/2008/12/11/dynamic-sorting-with-linq.aspx
This blog shows a good example of sorting. http://weblogs.asp.net/davidfowler/archive/2008/12/11/dynamic-sorting-with-linq.aspx
这篇关于Linq to实体以动态/严格类型过滤实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!