generate sql query from entity framework - c#

I am trying to generate sql query using .ToTraceString() in entity framework 3,5;
but I am getting the following error
Cannot convert type 'System.Collections.Generic.List to 'System.Data.Objects.ObjectQuery'
I have the following code
I need to generate sql query
customers= ent.Customer
.Include("UserType")
.Include("User")
.Where(c => c.Enabled == true)
.ToList<Customer>();
How to get sql query generated by entity framework. I don't have sql profiler.
Many thanks

Try not to call ToList() prior calling ToTraceString() because when you do you have list returned which doesn't have ToTraceString() method

Related

LINQ Correlated SubQuery throws Oracle Exception: Oracle 11.2.0.4.0 does not support APPLY

Here is the c# query:
qry = qry.Where(comment => idsArr.Any(
selectedId => dbv.VW_STAKEHOLDER_TYPE_XREF.Where(xref => xref.STAKE_ID == comment.STAKE_ID && xref.STAKEHOLDER_TYPE_ID == selectedId).FirstOrDefault() != null
));
From the language of the exception I am guessing that something in the c# is being translated to an apply which Oracle 11 doesn't like for some reason. I am not sure which part of the query is getting translated into APPLY and how I can circumvent this issue.
Thanks in advance.
LINQ to SQL only really works with SQL Server, although some simple queries may work on other databases. In this case the query failed as Oracle does not have a CROSS APPLY clause, but uses lateral joins instead. See this post for options when you want to use LINQ with Oracle: Is there a Way to use Linq to Oracle

Linq query with Entity Framework yields no result

I am doing a query with linq and entity framework. The database I'm using is Oracle. Here is the code :
Entities bdd = contextWrapper.GetContext();
data = (from table in bdd.COP_PRDTICSOURES
where (table.IDTTIC==ticketId && table.IDTPRD==productId)
select table).AsEnumerable();
When I look at the variable bdd.COP_PRDTIC_SOURES using a debugger, it contains an entry matching my two criteria. However, after the execution of the query, the data variable contains no result.
Is there something wrong with my syntax ?
Some additional information:
The entity i'm looking for isn't commited in the database when I perform this query. It's created before in the same transaction.
I use a foreach on the data after, so it's not a problem of lazy loading.
AsEnumerable does not load data from DB. Calling it you are not executing the actual query, just changing how it is going to be executed in its entirety.
Use ToArray or ToList instead to load data explicitly. Or call foreach on this collection.

Filtering IQueryable using self defined function causes runtime error

I had a similar query to LINQ: How to remove element from IQueryable<T> and for the most part it answered my question.
My code is set up similarly:
var items = MyDataContext.Items.Where(x =>MyFunction(x.value1, x.value2, x.value3));
...
...
bool MyFunction(decimal val1, decimal val2, decimal val3)
{
//some calculation with the parameters
return true;
}
It compiles fine, but when I run it, it throws an error:
"An exception of type 'System.NotSupportedException' occurred in
System.Data.Entity.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method
'Boolean MyFunction(System.Decimal, System.Decimal, System.Decimal)'
method, and this method cannot be translated into a store expression."
I'm new to linq, only started this week, so any help is appreciated. Thanks!
Your linq query is translated into SQL. In this case Linq to Entities doesn't know how to translate your method into SQL correctly. So you can't use custom methods on linq to sql queries. You can only use supported methods
If you want to do that, you have to fetch all data from database and do that in the memory.
if you want to run this code perfectly, you should use IEnumurable like this:
var items = MyDataContext.Items.toList().Where(x =>MyFunction(x.value1, x.value2, x.value3));
Try adding .ToList() before your where clause.
I am gussing you are querying a database of some sort, but the data provider can't translate your method Myfunction.
Get your data into memory before firing the where clause:
var items = MyDataContext.Items.ToList().Where(x =>MyFunction(x.value1, x.value2, x.value3)
From the cmoments I take it, there is some additional explanation needed. Your query is not executed until you really need it.
By calling .ToList() you materialize your data and every action afterwards is executed in memory on your machine instead on the database. This means you could very well split this into 2 calls:
//First call. Executed as SQL query on database. Materializes data into IEnumerable<Item>
var items = MyDataContext.Items.ToList();
//second call on client
var filteredItems = Items.Where(x => x.Id == MyFunction(value1, value2));
If you omit the ToList() call, Entity Framework trys to translate Myfunction() into SQL, which it can't and then throws an error.

Calling a UDF with Entity SQL but without a FROM clause?

Am trying to call a UDF which just takes a parameter and returns a scalar. The examples I've seen all use a From clause: http://blogs.microsoft.co.il/blogs/gilf/archive/2009/10/20/calling-user-defined-functions-udfs-in-entity-framework.aspx
But I only want to call the UDF, I don't want to "join" it with any entities using a from clause:
string fieldTag = "TagNameHere";
var sql = "SELECT XyzModel.Store.FieldNameToFormIdMap(#fieldTag)";
System.Data.Objects.ObjectQuery<int> query =
new System.Data.Objects.ObjectQuery<int>(sql, xyzEntitiesContext);
query.Parameters.Add(new System.Data.Objects.ObjectParameter("fieldTag", fieldTag));
I get the error: "The query syntax is not valid."
This is using .NET 3.5
I could use old style ADO.NET but I'd rather not have to manage another set of connection strings aside from those used for the entity framework.
Just remove SELECT from your query, like this:
var sql = "XyzModel.Store.FieldNameToFormIdMap(#fieldTag)";

How do I view the SQL generated by SubSonic SimpleRepository?

I've got this toy code, works fine, using MySQL
var r = new SimpleRepository("DB", SimpleRepositoryOptions.None);
var q = r.Find<User>(x => x.UserName == "testuser");
How do I view the SQL generated by that query ?
For SQL Server, you can always run SQL Profiler to see the queries.
Unfortunately using SimpleRepository you can't do what you want without stepping into the SubSonic code. Because the Find method returns an IList it's executed before you get the chance to evaluate the SQL that's going to be executed. There are efforts underway to add this functionality in future versions of SubSonic but until then you're probably best looking at the MySQL Query Profiler.

Categories