Afternoon,
How would i set this as not equal to? I am basically trying to say, if p.catrgory is not equal to one of the categories in the database.
p.category == dc.Categories.SingleOrDefault(c => c.Name == p.category).Name
Thanks in advance
p.Category is not in the DB?
var exists == !dc.Categories.Any(c => c.Name == p.category)
dc.Categories.Where(c => c.Name = p.category).Any();
it will return true if exists, so, use, ! operator
!dc.Categories.Where(c => c.Name = p.category).Any();
You should change:
p.category == dc.Categories.SingleOrDefault(c => c.Name == p.category).Name
to
p.category == dc.Categories.SingleOrDefault(c => c.Name != p.category).Name
if p.category is not equal to one of the categories in the databas
Related
I'm trying to add a "AND" condition to the join query but haven't been able to figure it out (not sure if it's even possible via Doctrine/Symfony). I'd appreciate any help with this.
->select('c, p')
->from(Customer::class, 'c')
->leftJoin('c.phones', 'p')
Example:-
SELECT c.*, p.*
FROM customer c
LEFT JOIN phone p ON c.id = p.customer_id AND p.is_main = 1 AND p.category = 0
You can use conditionType of the leftJoin function in queryBuilder check the documentation
public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null);
Example:
$qb->leftJoin('c.phones', 'p', 'WITH', 'p.is_main = 1 AND p.category = 0', 'p.id')
Use the WITH option like this:
$qb->leftJoin('c.phones', 'p', 'WITH', 'p.is_main = 1 AND p.category = 0', 'p.id')
Docs
I have 3 tables:
sessions - This store information about trainings
xref_session_faculty - This cross references the trainings and the teacher
user - list of all teachers
In one(or more) LINQ query i want to get all the sessions and for each session the teachers that will be conducting the training. Each session can have zero or more teachers in the DB.
sessions = db.sessions
.Where(x => x.seminar_id == seminarId)
.ToList()
.Select((x, i) => new fees
{
id = x.id,
sessionTitle = x.title,
teacherNames = "By:" + String.Join(",",
x.xref_session_faculty.ToList()
.Select(q => db.users
.Where(m => m.id == q.user_id)
.Select(t => t.firstName).ToList()
)
)
})
.ToList();
With this the teacherNames prints out By:System.Collections.Generic.List1[System.String],System.Collections.Generic.List1[System.String].
WHat is the right query format here?
teacherNames = "By:" + String.Join(",",
x.xref_session_faculty.ToList()
.Select(q => db.users
.Where(m => m.id == q.user_id)
.Select(t => t.firstName).FirstOrDefault()
)
)
you need to change ToList to FirstOrDefault function to get correct result
private var sessions = (from session in db.sessions.Where(x => x.seminar_id == seminarId)
select new
{
id = session.id,
sessionTitle = session.title,
teacherNames = (from faculty in db.xref_session_faculty.
where (x => x.session_id == session.id)
join us in db.uses on faculty.user_id equals us.user_id
select new
{
us.firstName,
other_field_names
})
});
How can I translate this to a lambda expression? Can anyone please help?
SELECT
p.*
FROM
db.MainProduct p
WHERE
p.id IN (SELECT ms.MainProductId
FROM db.MainProductToSupplierProduct ms)
AND (p.description LIKE '%' + #filter + '%'
OR Coalesce(#filter,'') = '')
Or in query syntax:
var ans = from p in db.MainProduct where (from ms in db.MainProductToSupplierProduct select ms.MainProductId).Contains(p.id) && (filter == "" || p.description.Contains(filter)) select p;
Not sure that is the most efficient way, a less literal translation would use a join:
var ans = from p in db.MainProduct join ms in db.MainProductToSupplierProduct on p.id equals ms.MainProductId where filter == "" || p.description.Contains(filter) select p;
This is a left semi join and the best way to implement that is probably an EXISTS in SQL - it may be faster than join:
var ans = from p in db.MainProduct where db.MainProductToSupplierProduct.Any(ms => ms.MainProductId == p.id) && (filter == "" || p.description.Contains(filter)) select p;
Below code can help you out
var filteredMainProduct= MainProduct.Where(t => MainProductToSupplierProduct.Any(z => z.MainProductId == t.id) && #filter != null ? t.description.Contains(#filter) : t.FareBasisDescription.Contains(""));
How to check for null in where condition in lambda expression?
listdropdownid.Id = listgroupid.Where(X => X.abc == desc).FirstOrDefault().abc_id.ToStr();
listdropdownid.Desc = desc;
If abc == desc fails, I need to assign null to listdropdownid.Id.
The most elegant way is to specify another default value using DefaultIfEmpty(newValue):
listdropdownid.Id = listgroupid
.Where(x => x.abc == desc)
.Select(x => x.abc_id.ToString())
.DefaultIfEmpty(null) // would be more useful if you'd provide a more meaningful value like "<not found>"
.First;
That works even with value types, in this case you could also use FirstOrDefault since the default value of string is already null. You just have to select it:
listdropdownid.Id = listgroupid
.Where(x => x.abc == desc)
.Select(x => x.abc_id.ToString())
.FirstOrDefault();
This should help you:
listdropdownid = listgroupid.Where(X => X.abc == desc).FirstOrDefault();
if (listdropdownid != null)
{
// Do something with listdropdownid.Id
}
do this
listdropdownid.Id = listgroupid.Where(X=>X!=null).Where(X => X.abc == desc).FirstOrDefault().abc_id.ToStr();
I have a query like this
SELECT id
FROM params
WHERE valid=1
AND id NOT IN (SELECT pid
FROM clientparams
WHERE update = 0 AND client=15)
LIMIT 25
I am trying to convert it to entity framework equivalent:
IQueryable<Params> parame = db.Params.Where(p => p.valid.Equals(1)).Except(....);
But I am stuck at the point where I need to make an equivalent for the subquery
Any ideas how to solve it?
Try this
IQueryable<Params> parame = db.Params.Where(p => p.valid.Equals(1) &&
!db.clientParams.Any(e => e.pid == p.id && e.update == 0 && e.client == 15))
.OrderBy(e => e.id) //Order by any field.
.Take(25);