how can use multiple fields in for each part - linq

I want to edit multiple records at once. Is it possible update multiple fields in for each part?
Return db.tblname.where (z=> z.id == model.id).toList().foreach(z=> z.name =model.nam && z.active=model. Active)
It dose not accept &&.

Shouldn't your for each do this?:
.foreach(z=> {z.name =model.nam; z.active=model.Active; });

Related

Laravel 5.5 - Check if only one column will be updated

Using Laravel 5.5 and models, I would like to know, before updated if only one column, statut, will be updated.
The interest is to just updated the statut if there are no changes or, if there are some columns to be updated, create a new row. Because each modification need to be approved before published.
Actually, I have a big if like :
if($request->statut != $product->statut && $request->title == $product ...)
Is there a quickest way ?
Thanks.
You can utilize isDirty('field') method to check if the field was changed (and not yet saved). getDirty() will return you array of all such fields and their values.
If I understood your case correctly, then you can do:
if ($product->isDirty('statut')) {
if (count($product->getDirty()) == 1) {
...
}
}
First we check if field statut was changed for your $product. Then we just check if number of changed fields is 1 (meaning that there are no more changed fields than just statut).

LINQ Check for Nulls with OR

I have 2 values in table User: Address1, Address2. Both could be null. As part of a filter method, I am attempting something like the below:
var tempUsers = users.Where(q => q.Address1.ToLower().Contains(address.ToLower()) || q.Address2.ToLower().Contains(address.ToLower()));
This is returning a Null Reference Exception, and rightly so.
Linq queries need to be handled against null values
I would be attempting
null.ToLower() and null.Contains() within the query
What is the best way to go around it? If it was a simple 1 field Query, for e.g. just Address1, I would have simply filtered out all items with empty Address1, and continued normally in the second query. In this case, both fields are important to the filtering, as in, the input: address could be either in Address1 or Address2 of the User table.
I know this might not be possible in a 1 liner, but what is the best approach to take in terms of time and performance?
How about this:
var address = (GetAddressFromOuterWorld() ?? String.Empty).ToLower();
var tempUsers = users.Where(user => (user.Address1 ?? String.Empty).ToLower().Contains(address)
|| (user.Address2 ?? String.Empty).ToLower().Contains(address));
This definitely works with LINQ to Object, but probably fails with LINQ to SQL, but in that case you normally write user.Address1 == address || user.Addrss2 == address and your database uses a case-insensitive collate setting.
You can easily add null checks like this.
var tempUsers = users.Where(q =>
(!string.IsNullOrEmpty(q.Address1) && q.Address1.ToLower().Contains(address.ToLower())) ||
(!string.IsNullOrEmpty(q.Address2) && q.Address2.ToLower().Contains(address.ToLower())));

RethinkDB chaining/combining filters

I have two filters that I need to combine.
This is my primary filter:
r.db('items').table('tokens').filter(r.row('valid_to').gt(r.now()))
and this is my secondary filter.
.filter(r.row["processed"] == False)
How do I combine these?
Just chain them together!
r.db('items').table('tokens')
.filter(r.row('valid_to').gt(r.now()))
.filter(r.row["processed"] == False)
And you can keep chaining stuff after that.
Once you have the database set, you can use the filters to carry on your equation, such as:
$query = \r\table('payments')
->filter(\r\row('forwarded')->eq('1'))
->filter(\r\row('bad_callbacks_sent')->lt(6))
->filter(\r\row('confirmations')->le(7))
->run($this->conn);
You see I have the table set, which means I can continue doing queries for that table without re-defining that table.

Query using EF "Include" and including the related tables on Where

So far, I thought I could do:
var num = db.MyTable.Include(x => x.RelatedTable)
.Count( x.idTenant == CurrentTenantID && x.Active &&
x.RelatedTable.SomeProperty.Value == true);
This always return zero records.
Am I assuming wrongly that Including the RelatedTable I can use it in the where part?
By the way... the "SomeProperty" is Nullable, that is why the ".Value".
I'm using Entity Framework 4.1. (Database first)
Are you trying to get the number of records? If so, why do you even need the Include? Entity Framework will lazy-load the RelatedTable entity set for you when it evaluates your Count condition. Also, if SomeProperty is a bool?, you should check if it has a value before you check the value itself.
var num = db.MyTable.Count(x =>
x.idTenant == CurrentTenantID &&
x.Active &&
(x.RelatedTable.SomeProperty.HasValue &&
x.RelatedTable.SomeProperty.Value));
You don't need to use Include if you only want to access navigation property in Where part. Include is only used to fetch (eager load) related records together with the main record from database to your application but it doesn't make sense if you only want to count records.

Doctrine Email Validation triggers on empty form field

I don't want an empty email field in my form to trigger a
Doctrine_Validator_Exception. But, since Doctrine_Validator_Email uses
"is_null()" to check for empty (non-existing) values (and the POST
['email'] contains an empty string), it does.
Is this correct? Is there a simple way around this?
This also has an impact when trying to set a column as unique (trying
to store a second empty string in the column triggers the validator).
What am I missing?
Thanks in advance,
Erland Wiencke
(Symfony 1.4.1/Doctrine 1.2)
I ran into this same problem in our Doctrine 1.2 application. In my case, the fix ended up adding this workaround to the record's merge() method:
$this->email = trim($this->email);
if ($this->email == '') {
$this->email = null;
}
There are several ways, not tested, but seem to work alltogether or separately:
1) $this->validatorSchema['email'] = null;
or
2) $this->validatorSchema['email'] = sfValidatorPass();
or
3) $this->validatorSchema->setOption('allow_extra_fields', true);
or
4) Just set required attribute to false:
$this->widgetSchema['email'] = sfWidgetFormInputText(array('required' => false));

Resources