0

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

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

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

My EntityDataSource:

<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=eodbEntities" DefaultContainerName="eodbEntities" EnableFlattening="False" EntitySetName="Corporations" OnQueryCreated="EntityDataSource1_QueryCreated">
</asp:EntityDataSource>
daniel
  • 34,281
  • 39
  • 104
  • 158

2 Answers2

2

You can do it in markup:

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

Or

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

For more information you can read here

Update:

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")));
}
Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
0

first can you plz set you filter value in variable like this

var name ="myname";

and you write your query like this it's below

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;

i think this will help you.....

Rajpurohit
  • 1,951
  • 2
  • 16
  • 19
  • no this doesn't help me. I want to get all corporations, which have a tag with the name "myname". I don't know how to do the linq query – daniel Nov 02 '12 at 13:25
  • you can use this query var filtervilue= from t in entities.Tags where c.Tags.Name.Startwith("myname") select t . i think this will help you – Rajpurohit Nov 02 '12 at 13:52
  • no. because i don't get the right type from the entities.Tags query. I need ObjectQuery. There seems not to be a possibility to cast from ICollection or IQueryable to ObjectQuery. See my second try for this. – daniel Nov 02 '12 at 14:01