question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you create a CTE with an IList<> passed as a parameter?

See original GitHub issue

My function currently takes an IList<> as a parameter. I need to be able to convert it to a CTE that is joined on in a SQL statement.

When I try to convert it to an IQueryable object and convert it to a CTE like so:

var myQueryable = myList.asQueryable<T>();
var myCTE = myQueryable.AsCte();

But myCTE object doesn’t reflect having any values from the list.

var myQueryableType = myQueryable .GetType(); 

This shows that the type is System.Linq.EnumerableQuery as well.

The myCTE object also has the message: “There is no method ‘AsCte’ on type ‘LinqToDB.LinqExtensions’ that matches the specified arguments”.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:21 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, May 26, 2020

There are only four IQueryable sources supported in library

  • GetTable
  • FromSql
  • SelectQuery
  • AsCte

Any of them are complicated. I appreciate your interest but it can be really challenging task from the start. For example implementing CTE took about month of my free time to understand how to do that correctly. It is not an just create SQL AST which generates VALUES, but it is also good knowledge in our LINQ parser.

I’ll give you small example which problems needs to be solved: We have array

var data = new [] {
  new Some 
   { 
    Column1 = ..,
    Column2 = ..,
    ...
    ColumnN = ..,
   },
   ...
},

Let’s write query

var query = 
   from d in data.AsQueryable(db)
   join t1 in db.OtherTable1 on d.Colum1 equals t1.Id
   join t2 in db.OtherTable2 on d.Colum2 equals t2.Id
   select new { t1, t2 }

In this query, we have used only two fields from data array. So only two fields should be used in SQL generation. Another trick with reusing values from other table:

var query = 
   from m in db.Master
   from d in new Some[] {
     new Some{Column1 = m.Id, Column2 = 1},
     new Some{Column1 = m.Id2, Column2 = m.Value}
     new Some{Column1 = 1, Column2 = 2}
   }
   join t1 in db.OtherTable1 on d.Colum1 equals t1.Id
   join t2 in db.OtherTable2 on d.Colum2 equals t2.Id
   select new { t1, t2 }

Also If database do not support VALUES, we have to mimic it by UNION ALL A lot of nuances here. Really. That’s why i’m trying to prioritize this task implementation.

If you still interested, i can start pointing you in right direction.

0reactions
viceroypenguincommented, May 26, 2020

Yeah, I’m going to write a series of new tests for sure. Need to test that the constant array works for insert, update, delete, merge, and regular select. I’ll put in [ActiveIssue] tests for all of the same for the outer apply version, knowing those won’t work in v1.

Thanks for the add’l places to look for changes though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - Is there any way to pass parameters to CTE?
A CTE doesn't need parameters. It can't exist on its own, it's always part of a query so it already has access to...
Read more >
Is it possible to use a paramter in the with clause?
Your question is not very clear, but yes, you can refer to variables in a Common Table Expression (CTE), which I assume that...
Read more >
passing a variable to CTE in a view
Hi ,. I have a view which contains a CTE and I need to pass the parameter inside the CTE. our application is...
Read more >
DynamicQueryEngine Class
Creates a new Select Query which is ready to use, based on the query parameters specified. (Overrides DynamicQueryEngineBase.CreatePagingSelectDQ( ...
Read more >
How to avoid passing property names as strings using C# 3.0 ...
Referencing property names via strings is evil. Consider this simplistic example: private int _myProperty; public int MyProperty { get ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found