problem with SQL CE and orderby linq clause - linq

I am using linq with SQL CE, but for a simple query like this:
var points=from i in this.DomainBoundaryPoints orderby i.Index select i;
I get this error:
Could not find an implementation of the query pattern for source type 'System.Data.Linq.EntitySet<DAL.DomainBoundaryPoint>'. 'OrderBy' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'?
Does SQL CE support 'order by' TSQL clause? Where is the problem?

Of course SQL CE supports orderby...
Try to do what the error message tells you...
add a reference to System.Core if it's not there
add a using directive for System.Linq

Related

HQL Query starting with REPLACE

I have a query starting with REPLACE. When I use it directly in MySQL console everything is fine, so query is ok. However, when I put it in my code like this:
#Query("REPLACE INTO WeekAggregate...
There is an error:
web - 2016-10-05 10:35:44,297 [localhost-startStop-1] ERROR o.h.hql.internal.ast.ErrorCounter - line 1:1: unexpected token: REPLACE
antlr.NoViableAltException: unexpected token: REPLACE
How could I fix this? Is REPLACE not supported in HQL?
HQL is trasversal to underlying DBMS. So some functions you're using in your SQL can't be interpretated by HQL.
You have two ways:
change your HQL query (in this case you can rewrite your query with an DELETE/INSERT statement)
you can write your query in SQL and you can use a method createSqlQuery so the interpreter, runs the query as SQL native query.
I changed my query to native SQL query by using nativeQuery = true flag.

Linq to Sql Connectivity

I am trying to fire a simple query of Linq
var user = (from u in Users
where u.Username == 'ABC'
&& u.Password == 'ABC'
select u).SingleOrDefault();
My problem is that the table Users is inside my SqlServer 2008 R2. How I manage to call this table from Sql and use in my Linq Query. I google a lot on this topic but not find any satisfactory answer. Please help.
if i understand correctly you have to use Linq to sql class and choose your DataBase
you can use this tutorial http://www.codeproject.com/Articles/26657/Simple-LINQ-to-SQL-in-C
edit
you have to add new element to your project, in Data : choose classes LINQ to SQL and in server Explorer make the chose of your database
I found this tutorial for all steps

How to find a linq generated Sql query that appear in Sql Server Sloq Query log?

I have some performance problem regarding some of my queries. When I query Sql Server for a list of slow queries, I find some queries that were generated by Linq-To-Entities.
For example:
SELECT
[Project12].[OrderId] AS [OrderId],
[Project12].[OrderDate] AS [OrderDate],
[Project12].[OrderStatusId] AS [OrderStatusId],
[Project12].[Name] AS [Name],
[Project12].[C1] AS [C1],
[Project12].[ClientId] AS [ClientId],
[Project12].[ClientCode] AS [ClientCode],
[Project12].[TwoLetterCode] AS [TwoLetterCode],
[Project12].[Identifier] AS [Identifier],
[Project12].[StartDate] AS [StartDate],
[Project12].[Code] AS [Code],
[Project12].[C2] AS [C2],
[Project12].[C3] AS [C3],
[Project12].[C4] AS [C4],
[Project12].[C5] AS [C5]
FROM ( SELECT
[Project11].[OrderId] AS [OrderId],
[Project11].[ClientId] AS [ClientId],
[Project11].[StartDate] AS [StartDate],
[Project11].[Identifier] AS [Identifier],
....
I tried to use the differents fields to find where I was querying those fields, but I cannot find where.
What technique can be used to find which Linq query generated some sql?
I believe you can use the DataContext.Log method for Linq to SQL (I believe this will work for Linq to Entities as well): http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.log.aspx
Hook that up with a StreamWriter and you can see what your Linq is doing. You could then use this log to compare against what you get from a SQL profiler log and see what is doing what exactly to compared the two.
I also prefer to use LinqPad while debugging things in straight SQL. You can even import DLL's for entity framework directly into it to query off of.
http://www.linqpad.net/
I posted a number of techniques that would likely help isolate your LINQ performance issues at ThinqLinq.

Return objects missing records in many to many using EF 5.0

Given the schema below, I'm trying to build an EF query that returns Contacts that are missing required Forms. Each Contact has a ContactType that is related to a collection of FormTypes. Every Contact is required to have at lease one Form (in ContactForm) of the FormTypes related to its ContactType.
The query that EF generates from the linq query below works against Sql Server, but not against Oracle.
var query = ctx.Contacts.Where (c => c.ContactType.FormTypes.Select (ft => ft.FormTypeID)
.Except(c => c.Forms.Select(f => f.FormTypeID)).Any());
I'm in the process of refactoring a data layer so that all of the EF queries that work against Sql Server will also work against Oracle using Devart's dotConnect data provider.
The error that Oracle is throwing is ORA-00904: "Extent1"."ContactID": invalid identifier.
The problem is that Oracle apparently doesn't support referencing a table column from a query in a nested subquery of level 2 and deeper. The line that Oracle throws on is in the Except (or minus) sub query that is referencing "Extent1"."ContactID". "Extent1" is the alias for Contact that is defined at the top level of the query. Here is Devart's explanation of the Oracle limitation.
The way that I've resolved this issue for many queries is by re-writing them to move relationships between tables out of the Where() predicate into the main body of the query using SelectMany() and in some cases Join(). This tends to flatten the query being sent to the database server and minimizes or eliminates the sub queries produced by EF. Here is a similar issue solved using a left outer join.
The column "Extent1"."ContactID" exists and the naming syntax of the query that EF and Devart produce is not the issue.
Any ideas on how to re-write this query will be much appreciated. The objective is a query that returns Contacts missing Forms of a FormType required by the Contact's ContactType that works against Oracle and Sql Server.
The following entity framework query returns all the ContactIDs for Contacts missing FormTypes required by their ContactType when querying against both Sql Server and Oracle.
var contactNeedsFormTypes =
from c in Contacts
from ft in c.ContactType.FormTypes
select new { ft.FormTypeID, c.ContactID};
var contactHasFormTypes =
from c in Contacts
from f in c.Forms
select new { c.ContactID, f.FormTypeID};
var contactsMissingFormTypes =
from n in contactNeedsFormTypes
join h in contactHasFormTypes
on new {n.ContactID, n.FormTypeID} equals new {h.ContactID, h.FormTypeID}
into jointable
where jointable.Count()==0
select n.ContactID;
contactsMissingFormTypes.Distinct();

Nhibernate Generate wrong SQL for Oracle with locking

yesterday I've been trying to make this code work inspite the fact it's just working fine with nhibernate and SQL server but when it come to oracle it generate wrong sql
UnitOfWork.Current.CreateCriteria<Bill>().Add(Restrictions.IsEmpty("ReqId"))
.SetMaxResults(BatchSize).SetLockMode(LockMode.Upgrade).List<Bill>();
the generated SQL will something like
Select * from
(Select bill_0.id,bill_0.BillNo ...... from Bill bill_0 where bill_0.reqId is Not null )
where ROWNUM < 10 for UPDATE of bill_0.ID
so i wont run because the allies bill_o is defined inside the inner sql statement so who got the solution ?
the correct sql would be something like this which i tried and worked on oracle db
Select bill_0.id,bill_0.BillNo ...... from Bill bill_0
where bill_0.reqId is Not null and ROWNUM < 10 for UPDATE of bill_0.ID
Since, as you say, NHibernate is generating invalid Oracle SQL, I suggest you file a bug with the NHibernate people. The SQL would work if the in-line view had been assigned an alias of "bill_0", or if the FOR UPDATE clause didn't use a table alias ("for UPDATE of ID"). Whether you can modify your NHibernate calls to make either of these happen I'm afraid I have no idea.

Resources