Can this statement be written in one line? - coffeescript

Just for curiosity, can this function be written in one line, or at least the for part producing the same output?
getById = (id)->
for e in array
return e if e.id is id
Fiddle for playing.

getById = (id) -> return e for e in array when e.id is id

Try
getById = (id) ->
return e for e in array when e.id is id

Related

LINQ from objects Left Join

I have a linq query that works fine when I join two tables, but when I include another table, it does not return data. Please help me figure out what I am doing wrong.
First Linq returns data:
var q = (from c in _context.Complaint
join cl in _context.Checklist on c.COMP_ID equals cl.COMP_ID into clleft
from cls in clleft.DefaultIfEmpty()
orderby c.timestamp descending
select new
{
FileNum = c.FileNum
}).AsQueryable().Distinct();
return q;
When I add this table, no data returns
var q = (from c in _context.Complaint
join cl in _context.Checklist on c.COMP_ID equals cl.COMP_ID into clleft
from cls in clleft.DefaultIfEmpty()
join oim in _context.OIM_EMPLOYEE on cls.MonitorEnteredEmpID equals oim.EmpID into oimleft
from oims in oimleft.DefaultIfEmpty()
orderby c.timestamp descending
select new
{FileNum = c.FileNum
}).AsQueryable().Distinct();
return q;

The nested query is not supported: Entity Framework fails on grouped left joins

Consider this query:
from e in db.MyEntities
join o in db.MyOtherEntities
on new e.Foo equals o.Foo into others
from o in others.DefaultIfEmpty()
select new
{
Value1 = e.Value1,
Value2 = o.Value2
};
With this simple left join, Entity Framework fails on doing the following group
from e in query group e by new { } into g select g.Count()
That may seem obscure, but it's actually a common thing for automatic grid implementations to do with your queries.
I encountered this using DevExtreme's data library: Total summaries wouldn't work on queries with left joins.
What you get is a
NotSupportedException: The nested query is not supported. Operation1='GroupBy' Operation2='MultiStreamNest'
This works though:
from e in query.Take(1) select { Count = query.Count(), /* other aggregates */ }
And so does this:
from e in query group e by e.SomePropertyThatsActuallyConstant into g select g.Count()
There is a workaround. You can write your query like this:
from e in db.MyEntities
from o in db.MyOtherEntities.Where(o => o.Foo == e.Foo).DefaultIfEmpty()
select new
{
Value1 = e.Value1,
Value2 = o.Value2
};
Strangely it also works when you put a where clause after the join:
from e in db.MyEntities
join o in db.MyOtherEntities
on new e.Foo equals o.Foo into others
from o in others.DefaultIfEmpty()
where e.Value1 == e.Value1
select new
{
Value1 = e.Value1,
Value2 = o.Value2
};
The where clause condition must not be merely constants, I guess EF is smart enough to reduce it otherwise.

Jpa how to return query with many parameters

How to optimize this code, now i got only last result from cycle. toAdd - array with products id.
Query query = null;
for (long l : toAdd) {
query = entityManager.createQuery("select p from Products p where p.id =:idProd", Products.class);
query.setParameter("idProd", l);
}
return (List<Products>) query.getResultList();
You're executing the query only once, and at the time you're executing it, the value of its parameter is the last element of your array, since you execute it once you've looped though the whole array.
To fix the code, it should be
List<Product> allProducts = new ArrayList<>();
for (long l : toAdd) {
query = entityManager.createQuery("select p from Products p where p.id =:idProd", Products.class);
query.setParameter("idProd", l);
allProducts.addAll(query.getResultList());
}
return allProducts;
But this is very inefficient: it executes a query and thus makes a roundtrip to the database for every ID in the list, instead of executing a single query returning all the products at once. Why not just use a better query:
query = entityManager.createQuery("select p from Products p where p.id in :productIds", Products.class);
query.setParameter("productIds", toAdd);
return query.getResultList();
You can try using IN clause.
query = null;
query = entityManager.createQuery("select p from Products p where p.id IN :idProds", Products.class);
query.setParameter("idProds", toAdd);
return (List<Products>) query.getResultList();
Query query = null;
query = entityManager.createQuery("select p from Products p where p.id IN(:idProd)", Products.class);
query.setParameter("idProd", toAdd.toString());
return (List<Products>) query.getResultList();
Where toAdd.toString() should generate a coma separated string (e.g "12,24,45,16")

sql query into Linq

Please anyone can help me to write this sql query into Linq.
select
P.ID,
P.Name,
Set_selected=
case when exists(
select C.ClassifierID
from dbo.ProductClassifiers C
where C.ProductID=130 and C.ClassifierID=P.ID)
then 'Yes' else 'No' end
from dbo.Classifier P
var retVal = (from s in dataContext.ProductClassifiers
join k in dataContext.Classifier
on s.ClassifierId equals k.Id
where s.ProductId == 30
select new {write here what values you want to get like s.Id,k.Name etc}).ToList();
Here's an attempt:
var query = from p in dataContext.Classifiers
select new {
p.ID,
p.Name,
p.Selected = dataContext.ProductClassifiers
.Where(c => c.ProductID == 130 &&
c.ClassifierID == p.ID)
.Any()
};
(That will make the Selected property Boolean rather than Yes/No, but that's usually going to be easier to work with.)
You should look at what the translated SQL looks like though, and in particular what the query plan is like compared with your original.
Untested, but hopefully works:
var q = classifier.Select(
p => new {
p.ID,
p.Name,
Set_selected = productClassifiers
.Select(c => c.ProductID == 130 && c.ClassifierID == p.ID)
.Any() ? "Yes" : "No"
}
);
The code assumes that you have two IEnumerable<T> representing the Classifier and ProductClassifiers tables.

How to perform Linq to Entites Left Outer Join

I have read plenty of blog posts and have yet to find a clear and simple example of how to perform a LEFT OUTER JOIN between two tables. The Wikipedia article on joins Join (SQL) provides this simple model:
CREATE TABLE `employee` (
`LastName` varchar(25),
`DepartmentID` int(4),
UNIQUE KEY `LastName` (`LastName`)
);
CREATE TABLE `department` (
`DepartmentID` int(4),
`DepartmentName` varchar(25),
UNIQUE KEY `DepartmentID` (`DepartmentID`)
);
Assume we had a EmployeeSet as an employee container ObjectSet<Employee> EmployeeSet and a DepartmentSet ObjectSet<Department> DepartmentSet. How would you perform the following query using Linq?
SELECT LastName, DepartmentName
FROM employee e
LEFT JOIN department d
ON e.DepartmentID = d.DepartmentID
I would write this, which is far simpler than join and does exactly the same thing:
var q = from e in db.EmployeeSet
select new
{
LastName = e.LastName,
DepartmentName = e.Department.DepartmentName
};
You need to use the DefaultIfEmpty method :
var query =
from e in db.EmployeeSet
join d in db.DepartmentSet on e.DepartmentID equals d.DepartmentID into temp
from d in temp.DefaultIfEmpty()
select new { Employee = e, Department = d };