【问题标题】:Filter with EntityDatasource in ASP.NET webforms在 ASP.NET 网络表单中使用 EntityDatasource 进行过滤
【发布时间】:2012-11-02 11:47:35
【问题描述】:

我有两个具有多对多关系的类(通过实体框架提供):

Corporation which has the member Tags

Tag which has the member name

EntityDataSource 为我提供了我想在给定标记名中过滤的 ObjectQuery,但我不知道如何。我想获取所有标签名称为“myname”的公司。我不知道怎么做 linq 查询

当我查询实体时,很遗憾没有得到 Objectquery。

protected void EntityDataSource1_QueryCreated(object sender, QueryCreatedEventArgs e)
{
    // first try
    var corps = e.Query.Cast<Corporation>(); 
    // of course doesn't work, because oyu can't access a member (name) of a collection (Tags) 
    // i don't know the right linq expression for this
    e.Query = from c in corps where c.Tags.Name.Contains("myname") select c; 

    // second try
    var tags = from t in entities.Tags where t.Name.Contains("myname") select t;
    var filteredcorporations = from c in tags select c.Corporations;
    // does not work because it is not a ObjectQuery<Corporation>
    e.query = filteredcorporations; 
}

我的实体数据源:

<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=eodbEntities" DefaultContainerName="eodbEntities" EnableFlattening="False" EntitySetName="Corporations" OnQueryCreated="EntityDataSource1_QueryCreated">
</asp:EntityDataSource>

【问题讨论】:

  • 您能提供更多信息吗?您是否阅读过此主题:stackoverflow.com/questions/5456150/…
  • 我想用代码隐藏做一个更复杂的过滤器。
  • 你想静态过滤数据源还是什么?
  • 静态过滤是什么意思?我想获取所有标签名称为“myname”的公司。
  • 你能用 EntityDataSource 显示你的标记吗?

标签: c# asp.net linq entity-framework


【解决方案1】:

你可以在标记中做到这一点:

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=eodbEntities" 
    DefaultContainerName="eodbEntities" EnableFlattening="False" 
    EntitySetName="Corporations" Select=""
    Where="it.Tags.Name.Contains(@tagname)">
    <WhereParameters>
        <asp:ControlParameter DefaultValue="myname" DbType="String" Name="tagname"/>
    </WhereParameters>
</asp:EntityDataSource>

或者

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=eodbEntities" 
    DefaultContainerName="eodbEntities" EnableFlattening="False"    
    EntitySetName="Corporations" Select=""
    Where="it.Tags.Name.Contains(&quote;tagname&quote;)">   
</asp:EntityDataSource>

更多信息可以阅读here

更新:

您的查询无法在标记中完成:(然后试试这个:

protected void EntityDataSource1_QueryCreated(object sender, QueryCreatedEventArgs e)
{
    var productQuery1 = e.Query.OfType<Corporation>();
    e.Query = productQuery1.Where(c => c.Tags.Any(t => t.Name.Contains("myname")));
}

【讨论】:

  • 不,正如我提到的 t.Tags.Name.Contains(@tagname) 是错误的查询。您无法访问集合(标签)的成员(名称)
  • 就是这样!非常感谢。我仍然需要理解那些 lambda 表达式和任何东西,包括等。
  • 感谢您使用 lambda 表达式。
【解决方案2】:

首先你能不能像这样在变量中设置过滤值

var name ="myname";

你这样写你的查询它在下面

var filtervilue= from t in entities.Tags where (name==""? name.Contains(t.Name):true) select t


var filteredcorporations = from c in filtervilue select c.Corporations;        
    e.query = filteredcorporations;

我想这会对你有所帮助.....

【讨论】:

  • 不,这对我没有帮助。我想获取所有标签名称为“myname”的公司。我不知道怎么做 linq 查询
  • 你可以使用这个查询 var filtervilue= from t in entities.Tags where c.Tags.Name.Startwith("myname") select t 。我认为这会对你有所帮助
  • 没有。因为我没有从entities.Tags 查询中得到正确的类型。我需要 ObjectQuery。似乎不可能从 ICollection 或 IQueryable 转换为 ObjectQuery。请参阅我的第二次尝试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多