How to use "dynamic queries" with Dynamic columns #964 feature?
See original GitHub issueHi, it seems that
var filteringColumn = "FirstName";
var sortingColumn = "LastName";
var filter = "John";
db.GetTable<Person>()
.Where(p => Sql.Property<object>(p, filteringColumn) == filter)
.OrderBy(p => Sql.Property<object>(p, sortingColumn))
.ToList();
from current #964 feature implementation is actualy not “fully implemented”. “Only constant strings are alowed for member name in Sql.Property expressions.”. Will this be implemented in the future.
I have entity
[Table("PRODUCT")]
public partial class Product
{
...
public double PRICE1 { get; set; }
public double PRICE2 { get; set; }
public double PRICE3 { get; set; }
public double PRICE4 { get; set; }
public double PRICE5 { get; set; }
...
}
//I want to query with
string whichPriceName = "PRICE" + settings.PriceID.ToString();
from p in dbContext.Db.GetTable<Product>()
select new ProductDTO
{
ID = p.ID,
PRICE = Sql.Property<double>(p, whichPriceName )
};
//Possible (current) solution to my problem is using expression method in Product Entity
[ExpressionMethod("WhichPriceExpression")]
public double WhichPrice(int priceID)
{
//local
switch (priceID)
{
case 5:
return PRICE5;
case 4:
return PRICE4;
case 3:
return PRICE3;
case 2:
return PRICE2;
default:
return PRICE1;
}
}
public static Expression<Func<Product, int, double>> WhichPriceExpression()
{
return (product, index) => index == 5 ?
product.PRICE5: index == 4 ?
product.PRICE4: index == 3 ?
product.PRICE3: index == 2 ?
product.PRICE2: product.PRICE1;
}
from p in dbContext.Db.GetTable<Product>()
select new ProductDTO
{
ID = p.ID,
PRICE = p.WhichPrice(settings.PriceID)
};
The expression method is actually translated to SQL CASE statement which is not nice, if there are a lot of PRICE{X} fields…
Is technically possible in Linq2DB to have expression method with parameter…
public static Expression<Func<Product, double>> WhichPriceExpression(int priceID)
{
switch (priceID)
{
case 5:
return (product) => product.PRICE5;
case 4:
return (product) => product.PRICE4;
case 3:
return (product) => product.PRICE3;
case 2:
return (product) => product.PRICE2;
default:
return (product) => product.PRICE1;
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
why I need a dynamic query for "column value from a query"
You need "dynamic SQL" because SQL will NOT allow you to use a parameter as a column name or a table name. You...
Read more >Dynamic Column Selection in Power Query
How to select columns based on a condition in Power Query. ... Query for a while, you'd be familiar with the Remove Other...
Read more >Building Dynamic Table-Driven Queries
One technique I use for minimizing maintenance is making my queries dynamic. Dynamic queries, while not right for every situation, ...
Read more >Dynamic Columns - MariaDB Knowledge Base
Dynamic columns allow one to store different sets of columns for each row in a table. It works by storing a set of...
Read more >Dynamic Column Names in Power Query - YouTube
Learn how to create dynamic column names in Power Query using a mix of Table.RenameColumns() and Table.ToColumns() function.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Unfortunately yo can not use this feature in that way. I will extend it. But for your case i have created working solution using
Sql.Extension
:usage
Not so trivial but before i fix Dynamic Columns it’s only one way. As you can see this method can be easily adopted to accept any column names.
should be fixed by #1102