Oracle IN clause with tuples using NHibernate components - oracle

I've declared some entity (named Employee) with FluentNHibernate mapping using CompositeId and ComponentCompositeIdentifier. My component identifier (named Person) consists of two fields:
LastName
FirstName.
When I look for employees using single person, everything works great. I need to look for employees by list of persons:
var result = from em in Session.Query<Employee>() where persons.Contains(em.Person)
Is NHibernate able to generate some sort of tuple expressions, like that? I'm using Oracle database.
SELECT * FROM Employees WHERE (FirstName, LastName) IN (('Guy', 'Himuro'), ('Karen', 'Colmenares'));

It's very likely you need https://github.com/nhibernate/nhibernate-core/pull/2159 to make it work. So currently it's not supported in NHibernate 5.2.

Related

How to create a subquery that uses listagg in JPA repository?

Using JPA specification classes or predicate builder. How can I convert this WHERE clause?
I am using an oracle db.
WHERE (SELECT listagg(reject_cd,':') within group (order by order_no) as rejectList
FROM REJECT_TABLE WHERE ID = transactio0_ id group by id) like '%06%'
The LISTAGG function is highly specific to Oracle, and is not supported by JPQL. However, you can still use a native query here, e.g.
#Query(
value = "SELECT ... WHERE (SELECT LISTAGG(reject_cd,':') WITHIN GROUP (ORDER BY order_no) AS rejectList FROM REJECT_TABLE WHERE ID = transactio0_ id GROUP BY id) LIKE '%06%'"
nativeQuery = true)
Collection<SomeEntity> findAllEntitiesNative();
Another option here might be to find a way to avoid needing to use LISTAGG. But, we would need to see the full query along with sample data to better understand your requirement.

Dynamics CRM 2011 Linq Left Outer Join

I am trying to get all records from an entity that do not join to another entity.
This is what I am trying to do in SQL:
SELECT * from table1
LEFT join table2
ON table1.code = table2.code
WHERE table2.code IS NULL
It results in all table1 rows that did not join to table2.
I have it working with Linq when joining on one field, but I have contact records to join on firstname, dob, and number.
I have a "staging" entity that is imported to; a workflow processes the staging records and creates contacts if they are new.
The staging entity is pretty much a copy of the real entity.
var queryable = from staging in linq.mdo_staging_contactSet
join contact in linq.ContactSet
on staging.mdo_code equals contact.mdo_code
into contactGroup
from contact in contactGroup.DefaultIfEmpty()
// all staging records are selected, even if I put a where clause here
select new Contact
{
// import sequence number is set to null if the staging contact joined to the default contact, which has in id of null
ImportSequenceNumber = (contactContactId == null) ? new int?(subImportNo) : null,
/* other fields get populated */
};
return queryable // This is all staging Contacts, the below expressions product only the new Contacts
.AsEnumerable() // Cannot use the below query on IQuerable
.Where(contact => contact.ImportSequenceNumber != null); // ImportSequenceNumber is null for existing Contacts, and not null for new Contacts
Can I do the same thing using method syntax?
Can I do the above and join on multiple fields?
The alternatives I found were worse and involved using newRecords.Except(existingRecords), but with IEnumerables; is there a better way?
You can do the same thing with method calls, but some tend to find it harder to read since there are some LAMBDA expressions in the middle. Here is an example that shows how the two are basically the same.
I've seen others ask this same questions and it boils down to choice by the developer. I personally like the LINQ approach since I also write a bunch of SQL and I can read the code easier.

How can I use Linq to join between objects and entities?

I have a collection of IDs in memory, and I would like to fetch only rows from a DB matching those IDs.
In SQL, I could either write a query like SELECT * FROM mytable WHERE id IN (1,3,5,10) or do a join between tables.
My problem is that EF can't build a query where I join my EF-data with my local array or list.
(I'm using EF4.1, but I'm guessing the problem/solution would be similar in older versions, as well as with Linq-to-SQL.)
You can use Contains() with your ID collection myIDs to generate the equivalent WHERE id IN .. query:
var results = context.mytable.Where(x => myIds.Contains(x.Id));

Need help converting a Linq query (Silverlight RIA Entity - Oracle DB)

I have no problem with SQL, but I'm finding Linq a little confusing.
C#, .NET4, Silverlight, RIA services, Oracle DB (v?), VS2010 running Devart dotConnect 6.10.121.
I have a RIA Entity
public sealed partial class ProcessLogHdr : Entity
{
DateTime JobDate;
string InterfaceName;
int SuccessfulCount;
int FailCount;
int TotalCount;
}
There are more fields such as user, etc, that won't be applicable to this post.
There are many jobs that make up a process. Each job has an entry in this table, but the view I want is a date group by summary.
I will be calling context.Load on a query, where I pass in the start and end date, which in Oracle looks like this:
select
trunc(JobDate),
InterfaceName,
sum(SuccessfulCount) as Total_Pass,
sum(FailCount) as Total_Fail,
sum(TotalCount) as Total,
max(JobDate) as Last_Msg_Processed_At_DT
from
ProcessLogHdrsEntity
where
JobDate >= START_DATE_IN_VAR and
JobDate <= END_DATE_IN_VAR
group by
trunc(JobDate),
InterfaceName
order by
trunc(JobDate) desc,
InterfaceName asc;
conttext.Load will call the linq query from a method that returns IQueryable.
The linq statement must run for Oracle under Devart dotConnect for Oracle.
I'm guessing I need some custom class to hold the results, like ProcessLogHdrDateSummary.
If you guys could help me fill in the missing ????? linq, I would be so grateful:
public IQueryable<ProcessLogHdrDateSummary> GetProcessLogHdrsDateSummary(DateTime START_DATE_IN_VAR, DateTime END_DATE_IN_VAR)
{
return ?????
}
Many Thanks!
There's no simple answer to this. One of the characteristics of LINQ to database providers is that some queries execute immediately, while others don't. Aggregation functions (MAX, MIN, etc) return immediately. So do some LINQ functions that specify particular output, such as .First(). Anything returning a collection will likely not execute immediately, and will return an IQueryable<> of some type.
What type? That depends on what the select clause of the LINQ statement specifies (which is not the same as the generated SQL select clause). "from c in db.customers select c" returns customer objects, but you can also use the select clause to populate other classes, or anonymous classes.
If a LINQ query returns an IQueryable<>, remember that the query hasn't executed yet! It won't execute until you start processing the data. You must process the data while still in scope of the data context, because once that's gone, you've lost your database connection.
You can always force an IQueryable<> to execute by ending it with .ToList(), .ToArray(), .ToDictionary(), or a few others. The List<> will use the same generic type as the IQueryable<> and the select clause (or .Select() method) of the LINQ statement.
The LINQ query will be rather complicated. I recommend that you follow these steps:
1. Create a stored procedure with an out cursor:
CREATE PROCEDURE myQuery(
DATE START_DATE_IN_VAR,
DATE END_DATE_IN_VAR,
cur out sys_refcursor) AS
BEGIN
OPEN cur FOR SELECT
trunc(JobDate),
InterfaceName,
sum(SuccessfulCount) as Total_Pass,
sum(FailCount) as Total_Fail,
sum(TotalCount) as Total,
max(JobDate) as Last_Msg_Processed_At_DT
from
ProcessLogHdrsEntity
where
JobDate >= START_DATE_IN_VAR and JobDate <= END_DATE_IN_VAR
group by
trunc(JobDate), InterfaceName
order by
trunc(JobDate) desc, InterfaceName asc;
END;
2. Add this procedure to the model. If you are using Devart Entity model, the return type will probably be generated. In other case you will need to create an entity or a complex type that will represent the return type of the generated method.
3. Treat the method call as a usual DomainService method.

Adding a random Guid column to a Linq to Entities query to grab random records

I've found some articles about using the RandomView view and the GetNewID function to pull back randomized records, but they are using this method with Linq to SQL which allows Functions and Stored Procs to be used with no return value or a scalar return value. From what I understand, a Stored Proc has to been returned as one of the Entity Framework objects from my generated model. I have been able to get that to work as an object, but not returning a scalar or no return set.
What I want to do is to simply add a column to my Linq query that contains a newly generated Guid so that I can order by the new Guids and take a specific number of records randomly. Can anyone help with some sort of lambda expression or join that would enable me to do this? It seems like it should be something built into EF, but I understand that we are on EF v1.
(Please provide code in VB.net)
In the Select clause of your Linq query, you should be able to insert a GUID like this:
var result = from myRecord in myTable
select new {
field1 = myRecord.field1,
field2 = myRecord.field2,
guidField = Guid.NewGuid()
};
Well, my VB is a little rusty, but I think this will work...
Dim result =
From myRecord in myTable _
Select field1, _
field2, _
guidField = System.Guid.NewGuid()

Resources