LINQ to Entities - DISTINCT on one column - linq

Is there a way to write following query with Linq to Entities (or Entity SQL, or Method Syntax, or any other way, but I would like to achieve it with Linq to Entities):
SELECT DISTINCT Column1
FROM Table1
I'm using Entity Framework 4. Of course I don't want to use Distinct method that filters data after data is fetched from database.
thanks,Pawel

Use something like
db.Table1.Select(t => t.Column1).Distinct()
As Munim mentioned in his comment, the Distinct() method does add the DISTINCT to the query. So resulting SQL query will be
SELECT [Distinct1].[Column1] AS [Column1]
FROM ( SELECT DISTINCT
[Extent1].[Column1] AS [Column1]
FROM [dbo].[Table1] AS [Extent1]
) AS [Distinct1]

For distinct by column, use this extension:
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property)
{
return items.GroupBy(property).Select(x => x.First());
}

Related

List of multiple column condition in query (kind of batch)

when trying to search with single record then this query works
#Query(value = "select * from table t where t.column1 = :column1 and t.column2 = :column2 and t.column3 = :column3")
Flux<Invoice> findByMultipleColumn(#Param("column1”) String column1, #Param("column2”) String column2, #Param("column3”) String column3);
But when I have list of criterias instead of a single row condition then I have to loop over the list of criterias & call the above query multiple times which is not feasible solution.
Sudo code
for (Criteria criteria : criteriaList) {
repository.findByMultipleColumn(criteria.getColumn1(), criteria.getColumn2(), criteria.getColumn3());
}
What I am trying to find a way to solve the above query for multiple LIST of all the 3 column criteria pair, something like below (this is not working solution)
#Query(value = "select * from table t where t.column1 = :column1 and t.column2 = :column2 and t.column3 = :column3")
Flux<Invoice> findByMultipleColumn(#Param List<Table> table);
Is there any way somehow we can try to achieve the above case?
Would be doable if column1, 2 and 3 were Embedded, then you could do
#Query(select * from Entity where embeddedProperty in (:values))
Flux<Entity> findByEmbeddedPropertyIn(Collection<EmbeddedClas> values);
Which would generate the following native SQL clause
Where (column1, column2, column3) in ((x, y, z), ...)
If you don't want to pack these fields i to an embeddable class, you can also try to do a workaround
#Query(select * from Entity where Concat(column1, ';', column2, ';', column3) in (:parametersConcatrenatedInJava)
Flux<Entity> findBy3Columns(Collection<String> parametersConcatrenatedInJava);
It's ofcourse not bulletproof, all three columns could have ";" as their values, this might be problematic if their type is not string, etc.
Edit.:
Third option is to use specification api. Using the criteria builder you can concatenate multiple and / or queries. And pass that specification as an argument to the repository that extends JpaSpecificationExecutor (if you're fetching whole entities) or an entity manager if you're using projections. Read more about specifications

Need help understanding how to convert sql statement to (Linq) or (Linq To SQL)

Hi I need some help coverting this sql statement to Linq, I am very new to Linq and LinqToSql, and this is my weakness, it seems like this is used frequently and I need to wrap my brain around the syntax. The Code is below.
select distinct t1.Color from [ProductAttributes] t1 join [Product] t2 on t1.Name = t2.ProductName where t1.ProductID = #productID order by t1.color
#productID is the parameter coming into the function, where I am trying to use Linq in MVC.
Thanks
It might be like this I guess
int myProductID = 1;//or whatever id you want.
MyDataContext mdc = new MyDataContext(CONNECTION_STRING_IF_NEEDED);
//MyDataContext is your datacontext generated by LinqToSql
var result = (from x in mdc.ProductAttributes
join y in Products on x.Name.equals(y.ProductName)
where x.ProductID = myProductID
orderby x.color
select x.Color).Distinct();
Note That Table names might need to be fixed.

How can I convert a sql query to Linq query?

SELECT DISTINCT Title,
ProductDescription,
COUNT(1) as Duplicate
FROM DB_Deals
GROUP BY Title, ProductDescription
HAVING COUNT(1) > 1;
Well, if by EF, you mean making a query using LINQ to Entities...
from deal in context.DB_Deals
group deal by new { deal.Title, deal.ProductDescription } into dealGroup
where dealGroup.Count() > 1
select new {
dealGroup.Key.Title,
dealGroup.Key.ProductDescription,
Duplicate = dealGroup.Count(),
}
Assuming, context is your DbContext, and DB_Deals is your mapped table name.
See
Entity Framework T-Sql "having" Equivalent
Group By Multiple Columns

Doctrine : subquery in DQL

I'm trying to calculate a value using DQL on one single table. Say this table is named "TABLE". It has 5 colums :
id
people_count
animal_count
region_id
type_id
The result I'm looking for is the sum of people (when grouped by region), divided by the sum of animals (when grouped by type);
The SQL would be something like that :
SELECT SUM(people_count) /
(
SELECT SUM(animal_count)
FROM TABLE t2
GROUPED BY type_id
)
FROM TABLE t1
GROUPED BY region_id
How would you do that in Doctrine using DQL?
I resolved my problem by creating another query, executing it and including the result in the first one.
This is probably not the best way to do if you are dealing with a simple example, but it was the only solution for me regarding to my code architecture.
I have a solution but I think there is probably a best solution to resolve your problem.
In any case, make two queries and import results of first query in the second can be a solution. Unfortunately, it's a low-usage for our database. More, sometimes we must execute only one SQL to sort on column result per example.
<?php
namespace AppBundle\Repository;
use AppBundle\Entity\MyClass;
use Doctrine\ORM\EntityRepository;
class MyRepository extends EntityRepository
{
public function findWithSubQuery()
{
return $this->createQueryBuilder('a')
->addSelect(sprintf(
"(SELECT COUNT(b.id) FROM %s AS b WHERE b.a = a.id GROUP BY a.id) AS otherColumn",
MyClass::class
))
;
}
}
I use this solution. Maybe the subquery could be write with DQL ojbect rather that DQL string.

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.

Resources