What is better?
Use:
GridView.DataSource = ObjectFromDB.All().Where(a => a.ID == null);
Or:
GridView.DataSource = ObjectFromDB.Find(a => a.ID == null);
?
Related
I have a method below pulling specific fields that have null values.
Every record in the table has a status field of 1 or 0. 1 is active and 0 is inactive.
For the query below I only want to pull data for active records i.e status = 1
How can I modify this query to put in the condition for status = 1?
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}
You can chain two Where Linq extension method calls right after one another, like so:
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Where(t => t.status == 1) // added this line...
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}
you should just be able to change the where condition to the below
.Where(t => (t.FirstName == null || t.LastName == null || t.EmailAddress == null || t.Telephone == null) && t.status == 1)
The && means and, so its saying those fields are null and the status equals 1.
I want to use nested operation in Handlerbars like
if(value == "a" || value == "b")
How can I create this equations using handlebar.
Thanks in advance
Create helper (I use underscore.js for simplicify):
eq.js
Handlebars.registerHelper('eq', function() {
var i, options, val1, vals;
val1 = arguments[0], vals = 3 <= arguments.length ? slice.call(arguments, 1, i = arguments.length - 1) : (i = 1, []), options = arguments[i++];
return _.any(vals, function(val) {
return val1 === val;
});
});
or eq.coffee
Handlebars.registerHelper 'eq', (val1, vals..., options) -> _.any vals, (val) -> val1 is val
And use:
{{eq value 'a' 'b'}}
I trying to build dynamic filters using findAll on a list. I have a variable that needs to be included in the filter only if not null.
#Test
void testSample(){
def list = [ new Employee(age:22, isManager:false),
new Employee(age:23, isManager:true),
new Employee(age:22, isManager:true) ] as Set
def var = 22;
String query1 = "it.age == var && it.isManager == true "
String query2 = "it.isManager == true"
println list
println list.findAll { var ? query1 : query2 } // Should give 1 record age = 22 and manager
var = null;
println list.findAll { var ? query1 : query2 } // should give 2 records-only manager
}
Both of them giving all the records. Is there anyway I can achieve this in one condition without need to write muiltiple queries ?
Looking some like below (this doesn't work though)
println list.findAll{
if(var) it.age == var &&
it.isManager == true
}
Try with Closures rather than Strings describing what you want to do:
def list = [ new Employee(age:22, isManager:false),
new Employee(age:23, isManager:true),
new Employee(age:22, isManager:true) ] as Set
def var = 22;
Closure query1 = { it.age == var && it.isManager == true }
Closure query2 = { it.isManager == true }
println list
println list.findAll( var ? query1 : query2 ) // Should give 1 record age = 22 and manager
var = null;
println list.findAll( var ? query1 : query2 ) // should give 2 records-only manager
Edit
Do you mean:
println list.findAll{ ( var ? it.age == var : true ) && it.isManager == true }
Or better:
println list.findAll{ ( var != null ? it.age == var : true ) && it.isManager == true }
Can you provide an example of batch_mutate() function in phpcassa?
Cant understand how to work with this function and didnt found any enough information.
Also i want to know how to use it with counters
Thanks in advance.
"delete data from multiple keys":
function batch_remove($key=null, $columns=null, $super_column=null, $write_consistency_level=null) {
$timestamp = CassandraUtil::get_time();
$deletion = new cassandra_Deletion();
$deletion->timestamp = $timestamp;
if ($super_column !== null) $deletion->super_column = $this->pack_name($super_column, true);
else $deletion->super_column = null;
if ($columns !== null) {
$predicate = $this->create_slice_predicate($columns, '', '', false, self::DEFAULT_COLUMN_COUNT);
$deletion->predicate = $predicate;
}
$mutation = new cassandra_Mutation();
$mutation->deletion = $deletion;
if (is_array($key) && count($key) >= 1) {
$mut_map = array();
foreach($key as $v) {
$packed_key[$v] = $this->pack_key($v);
$mut_map[$v] = array($this->column_family => array($mutation));
}
return $this->pool->call("batch_mutate", $mut_map, $this->wcl($write_consistency_level));
} else return false;
}
//delete name1, name2, name3 from key1, key2 in a single call
$column_family->batch_remove(array(key1, key2), array(name1, name2, name3));
batch_mutate on counters is not available yet with PHPCassa: https://github.com/thobbs/phpcassa/issues/31
batch_mutate in action, as per the tutorial: http://thobbs.github.com/phpcassa/tutorial.html
"inserting data":
$row1 = array('name1' => 'val1', 'name2' => 'val2');
$row2 = array('foo' => 'bar');
$column_family->batch_insert(array('row1' => $row1, 'row2' => $row2);
Scenario:
Lets say we got to check for address lines. which includes addressline1, addressline2,Town,Country,Postcode
If any one of the property is entered, all other fields are mandatory.
If none of it is entered, the validation doesnt have to get trigged.
To achieve it, I ended up with two lines of If statement.
Like
if(AddressLine1 != null || AddressLine2 != null || Town != null || Country != null)
{
if(AddressLine1 != null && AddressLine2 != null && Town != null && Country != null) == false
{
return false;
}
}
Note: I am using c#. Are there any language constructs i can make use of.
private bool IsAddressValid(params string[] addressParts)
{
return addressParts.Any(p => p != null) ? addressParts.All(p => p != null) : true;
}
To be called like so:
var addressValid = IsAddressValid(AddressLine1, AddressLine2, Town, County);
Well, the null-coalescing operator can help with the first:
if (AddressLine1 ?? AddressLine2 ?? Town ?? Country != null)
{
if (AddressLine1 == null || AddressLine2 == null ||
Town == null || Country == null)
{
return false;
}
// Presumably there's more here
}
You might want to write some helper methods though:
if (IsAnyNonNull(AddressLine1, AddressLine2, Town, Country))
{
if (IsAnyNull(AddressLine1, AddressLine2, Town, Country))
{
return false;
}
}
Where the utility methods would be something like:
public static bool IsAnyNonNull(params object[] values)
{
return values.Any(x => x != null);
}
public static bool IsAnyNull(params object[] values)
{
return values.Any(x => x == null);
}
Of course, you've still got two if statements - but I think that's basically necessary here anyway.
If you make an array of the fields in the group, then you can do:
var fields = new object[] {AddressLine1, AddressLine2, Town, Country};
return fields.All(f => f == null) || fields.All(f => f != null);
Define this:
public static bool SameNullness(params object[] values)
{
int nullCount = 0;
foreach (var value in values)
{
if (value == null) nullCount++;
}
return nullCount == values.Length;
}
Then use it like:
SameNullness(AddressLine1, AddressLine2, Town, Country);