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.

TEntity cannot convert to SQL

See original GitHub issue

Describe your issue.

Hi, i want to update database object full dynamically but linq2db cannot create sql from TEntitiy

here is my function

public async Task UpdateEntity(Expression<Func<TEntity, bool>> predicate, TEntity entity)
{
  await Context.GetTable<TEntity>().Where(predicate).UpdateAsync(x => entity);
}

Cann someone help me to fix this issue?

Exception message:
Stack trace: LinqToDB.Linq.LinqException : 'value(AlfaTools.Interface.GDI.Data.GDIRepository`1+<>c__DisplayClass12_0[AlfaTools.Interface.GDI.KUNDEN]).entity' cannot be converted to SQL.

Environment details

linq2db version: 3.3.0 Database Server: Windows Server 2012 Database Provider: Firebird 3.0 Operating system: Windows .NET Framework: 5.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
sdanylivcommented, Sep 21, 2021

Use Expression, we cannot get this information without ExpressionTree

await UpdateCustomer(1202579, x => new KUNDEN() { BEMERK2 = updatevalue });
2reactions
sdanylivcommented, Sep 21, 2021

Well, this one should work, as you have shown previously. Also added dynamic variant

public async Task UpdateEntity(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpr) 
{
	await Context.GetTable<TEntity>().UpdateAsync(predicate, updateExpr);
}

public async Task UpdateEntity(Expression<Func<TEntity, bool>> predicate, TEntity entity) 
{
	var ed = Context.MappingSchema.GetEntityDescriptor(typeof(TEntity));
	var updateColumns =
		ed.Columns.Where(c => !c.SkipOnUpdate && !c.ShouldSkip(entity, ed, SkipModification.Update)).ToList();

	if (updateColumns.Count == 0)
		return;

	var param      = Expression.Parameter(typeof(TEntity), "e");
	var entityExpr = Expression.Constant(entity);
	var bindings = updateColumns.Select(c =>
		Expression.Bind(c.MemberInfo, Expression.MakeMemberAccess(entityExpr, c.MemberInfo)));

	var updateExpr = Expression.Lambda<Func<TEntity, TEntity>>(
		Expression.MemberInit(Expression.New(typeof(TEntity).GetConstructors().Single()), bindings), param);

	await UpdateEntity(predicate, updateExpr);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot convert Linq to SQL
ERROR im getting : "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.".
Read more >
Can't convert a WKT with a Z value into a SQL Geometry ...
So it looks like i was able to get around my issue by replacing any occurance i had of .AsText(). to .ToWKT().
Read more >
Why does SQL Server say it can't convert varchar to numeric?
A conversion error will occur at run time when an attempt is made to convert the sales.pid value 'Colgate' value to numeric(9) to...
Read more >
SAP DBTech JDBC: Cannot convert SQL type TABLE to ...
My Procedure is something like : PROCEDURE SAMPLE( IN EMP_DATA COM_SAP_EMP, IN ID INTEGER, IN AGE INTEGER, OUT RESULT TABLE("ABC" ...
Read more >
How to convert sql to EF linq query
I am converting a sql query with 2 table Joins to an entity Framework linq query in a .net6 application. The sql query...
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