Linq to Sql Connectivity - linq

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

Related

Generated group by sql query does not make sense

I'm using entity framework core for mysql, and i've been running a complex linq query which i'm trying to optimise.
I turned on logging in the mysql server to view the resulting queries from the linq queries.
Oddly, none of it made sense as my complex query that joined 5 tables and performed multiple group bys, where, and order by clause was registered in the logs as 5 separate select all columns from table statements.
So, I tried a simple group by statement for one table. The resulting sql log produced "Select all_columns from table_name order by groupbyid".
Can anyone explain what happened here?
Thanks in advance.
More info as requested:
Sql query:
var queryCommand = (from p in _context.TableExtract group p by p.tableExtractPersonId);
queryCommand.ToList();
Resulting mysql log after:
SELECT .... [very long list of column names]
FROM TableExtract AS p
ORDER BY p.tableExtractPersonId
I've tried two different entity framework libraries: MySql.Data.EntityFrameworkCore(v8.0.17) and Pomelo.EntityFrameworkCore.MySql (v2.2.20) with the same results. I've tried .net core 3.0 and also received the same results. I'm going to try .net standard next.
Ok. I found it:
var queryCommand = (from p in _context.TableExtract group p by p.tableExtractPersonIdinto g select g.Key)
Forces linq to evaluate as a SQL group by. Otherwise apparently it does it's own thing with the group by.

Query working in native Oracle sql but not through odbc. Alias from subquery is an invalid identifier

A collegue needs to work with this data in Excel. I wrote the query below. It runs fine when I run it from sql developer. But when I want to use it in Microsoft Query which apparently uses ODBC to connect to the Oracle database, I get an error that says that the identifier "due" is invalid.
But how can I name the sum from the subquery in the select part of the sql?
SELECT cl.clid, cl.cl_name, s.due, con.oid, con.contract_status
FROM clientinfo cl
LEFT OUTER JOIN
(SELECT clid, sum(dueamount) as due
from account GROUP BY clid) s
ON s.clid = cl.clid
LEFT OUTER JOIN contract con
ON con.clid = cl.clid
ORDER BY cl.clid
I translated the names into english so that the query makes a bit more sense to you. I want to show the client id and their names along with the due amount and an object number with the status of the contract.
Create a view in the Oracle DB and let your colleague query that view through ODBC.

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();

Query multiple oracle schemas in LINQPAD with IQ driver

Is it possible to query multiple schemas in Linqpad with the IQ connection driver for oracle?
E.G. I have a user that can see two schemas ,users and customers. Currently I have to specify the schema I want to use when setting up the connection. So, I can only query one schema per session. It would be great if I could do something like the following:
var customers = from c in *customers*.sometable
select c;
var users = from u in *users*.sometable
select u;
...
in a single session. Is such a thing possible with any existing oracle drivers in LINQPad?
This cannot be done at the moment: when the driver builds a typed datacontext, it creates objects for just one schema. Why don't you register a request in linqpad.uservoice.com - a few other people have asked about this.

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