Can LINQ query be mixed with a dynamic SQL to inject a WHERE clause?

Posts   
 
    
Posts: 2
Joined: 19-Feb-2018
# Posted on: 19-Feb-2018 06:37:23   

Hello,

I'm facing an issue that I'm hoping to get your input on. I use LINQ to chain multiple where clauses, which is working great. However, some tables in my DB are dynamically generated and I only know their name at run-time. However, they all have similar structure, so I'd like to construct a query that joins to those tables (they do not have entities generated, but I can safely project to a dummy entity). Basically, I'm looking for something like this:



using (var a = DataAccessAdapter)
{
    var x = new LinqMetaData(a);
    //This starts the base query
    IQueryable<EventEntity> q = from e in x.Event select e;
    
    //Add some where clauses based on generated entity attributes
    q = q.Where(z => z.Attribute1 == "abc"); //this is working great
    q = q.Where(z => z.Attribute2 == 123); //this is working great

    //Here I'd to join to a dynamic table to inject a custom WHERE clause. Is something like this possible? Table XYZ does not have a generated entity, but if I can project it to a dummy entity CustomTableEntity that has only one column ObjectId and join with the rest of the query, that would really help.

    q = q.Where(z => x.AdapterToUse.FetchQuery<CustomTableEntity>("select ObjectId from XYZ where col1=99").Where(c => c.ObjectId == z.Id).Any());
    
   var results = q.ToList();
}

Thanks in advance! Albert

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 19-Feb-2018 09:47:27   

No sorry, that's not supported.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 2
Joined: 19-Feb-2018
# Posted on: 19-Feb-2018 14:28:28   

Thank you for a quick response. Can you suggest any alternatives to achieve the same effect? For example, is it possible to modify raw SQL after it's been generated from expression tree, but before it is executed?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 19-Feb-2018 19:59:22   

Derive a class from DataAccessAdapter.

In there override the GetFieldPersistenceInfo* methods and call the base class' version first. In these overrides, you can manipulate the table names (SourceObjectName), the way you want.