Linq to Entities Filtering an Entity Dynamic/Strongly Typed

夙愿已清 提交于 2019-12-05 06:32:49

问题


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)

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?


回答1:


Have you tried to use a lambda expression?

grid.DataSource = myEntities.Customers.Where(c => c.Name == "Bob");

or in 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

This blog shows a good example of sorting. http://weblogs.asp.net/davidfowler/archive/2008/12/11/dynamic-sorting-with-linq.aspx



来源:https://stackoverflow.com/questions/784363/linq-to-entities-filtering-an-entity-dynamic-strongly-typed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!