0

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

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>
4

2 回答 2

2

您可以在标记中执行此操作:

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

有关更多信息,您可以在此处阅读

更新:

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

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")));
}
于 2012-11-02T14:39:44.983 回答
0

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

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;

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

于 2012-11-02T12:26:08.957 回答