在ASP.NET Webforms中使用EntityDatasource进行过滤 [英] Filter with EntityDatasource in ASP.NET webforms

查看:78
本文介绍了在ASP.NET Webforms中使用EntityDatasource进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类(通过实体框架给出),它们有很多关系:

I have two Classes (given via Entity Framework) which are in a many to many relationship:

Corporation which has the member Tags

Tag which has the member name

EntityDataSource给了我ObjectQuery我想要在一个给定的标记名过滤,但我不知道如何。我想得到所有的公司,其中有一个名称为myname的标签。我不知道如何做linq查询

The EntityDataSource gives me the ObjectQuery which i want to filter in a given tagname but i don't know how. I want to get all corporations, which have a tag with the name "myname". I don't know how to do the linq query

当我查询实体时,我不幸得不到一个Objectquery。

When i query the entities i unfortunately don't get an 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; 
}

我的EntityDataSource:

My EntityDataSource:

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


推荐答案

您可以在标记中执行:

<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

For more information you can read here

更新:

您的查询无法在标记中完成:(请尝试:

Your query cannot be done in markup:( Then try this:

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")));
}

这篇关于在ASP.NET Webforms中使用EntityDatasource进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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