Where clause using LIKE condition in MVC4 lambda - asp.net-mvc-4

I want to show results where w.DrawDate contains (LIKE %year%) the year.
return View(db.Euro.Select(p => p).Where(w => w.DrawDate == year).ToList());
As seen in Like condition in MVC4 using lambda, i should use the Contains() method, but it isn't available.
So, how can i make w.DrawDate == year into w.DrawDate LIKE %year%?

You already have the answer:
View(db.Euro.Where(w => w.DrawDate.Contains(year)).ToList());
And you're probably just missing a namespace

Related

Laravel Eloquent: sum with groupBy

Need eloquent/fluent query to get sum with groupBy function.
So far I have tried:
$this->data['no_of_pages'] = Document::sum('no_of_pages')
->groupBy('users_editor_id');
Which ofcourse gives me call to member function groupBy() on non-object because of the fact that sum() will already execute the query and have result ready by the time grouBy() is applied. So can anyone guide me?
Document::groupBy('users_editor_id')
->selectRaw('sum(no_of_pages) as sum, users_editor_id')
->pluck('sum','users_editor_id');
// originally lists(), which was deprecated in favour of pluck in 5.2
// and dropped completely in 5.3
// ->lists('sum','users_editor_id');
// returns array like this:
array(
users_editor_id => sum,
...
)
Or this way (which I wouldn't use, since it won't be actual ORM result):
Document::groupBy('users_editor_id')
->selectRaw('*, sum(no_of_pages) as sum')
->get();
// returns collection of Document pseudo models with additional sum field
Document::Where('some_condition',true)
->select([DB::raw("SUM(debit) as total_debit"), DB::raw("SUM(credit) as total_credit")])
->groupBy('id')
->get()
Little bit refactored and convenient answer is (from #keroles Monsef)
Document::Where('some_condition',true)
->selectRaw("SUM(debit) as total_debit")
->selectRaw("SUM(credit) as total_credit")
->groupBy('id')
->get();

acts-as-taggable-on tagged_with or_where?

I'm trying to query an OR WHERE into an acts-as-taggable-on query, like so...
Business.tagged_with(params[:query], :any => true)
But I'd also like to perform at the same time an or_where like this...
Business.tagged_with(params[:query], :any => true).or_where('name LIKE ?', "%#{params[:query]}%")
This obviously doesn't work as there is no or_where method but would someone know how to perform this correctly?
In short, I'm trying to find a match against any tags OR business name. Thanks.
You can OR two queries together by using the | operator like so:
Business.tagged_with(params[:query], :any => true) | Business.where('name LIKE ?', "%#{params[:query]}%")
Note that this will eager load the results, so you can't apply any more conditions after this, like ordering. It will return an array with all of the matching results in it, excluding duplicates.

Rails scope that does nothing for NOT IN values

I have a Rails 3 scope that excludes an array of ids.
What is the best way to write the scope so that it does nothing when the array is empty and is still chainable? I currently have this, which works, but seems a little hokey:
scope :excluding_ids,
lambda {|ids| ids.empty? ? relation : where('id not in (?)', ids) }
If I do not have the "ids.empty? ? relation :" bit, when ids is empty the SQL generated is
... ID not in (NULL) ...
which will always return nothing. So something like:
Model.excluding_ids([]).where('id > 0')
returns no results.
If the ids array is empty then don't return anything.
scope :excluding_ids, lambda { |ids|
where(['id NOT IN (?)', ids]) if ids.any?
}
The query will run without any additional constraints on the query if there are no ids.
In Rails 4 you can use:
scope :excluding_ids, ->(ids) { where.not(id: ids) }
Here's a slight variation on Douglas' answer, using ruby 1.9 stabby lambda syntax and without the brackets in the where method.
scope :excluding_ids, ->(ids) {where("id NOT IN (?)", ids) if ids.any?}
How about the following? (It still checks for an empty array though, so if that's what you're trying to avoid it's not much of an improvement :)
scope :excluding_ids,
lambda {|ids| (ids.empty? && relation) || where('id not in (?)', ids) }

Multiple calls to a Rhino mocked method return different results

If I want to mock a class that returns a string that is used to determine whether while loop should continue (imagine read while string != null), how can I set the expectation. I have tried the following:
provider.Reader.Expect(r => r.ReadLine()).Return("1,10,20");
provider.Reader.Expect(r => r.ReadLine()).Return(null);
but when it is called twice in the same method, it returns the first string on both occasions, whereas I want it to return the second value (null) if called a second time.
I think you can just stick the repeat on the end of the syntax you're currently using.
provider.Reader.Expect(r => r.ReadLine()).Return("1,10,20").Repeat.Once();
provider.Reader.Expect(r => r.ReadLine()).Return(null).Repeat.Once();
or
provider.Reader.Expect(r => r.ReadLine()).Return("1,10,20").Repeat.Once();
provider.Reader.Expect(r => r.ReadLine()).Return(null);
if you have any calls beyond 2nd call that you want to use second expectation.
I'm not familiar with the syntax you're using. I would write this as:
r.ReadLine();
LastCall.Return("1,10,20").Repeat.Once();
r.ReadLine();
LastCall.Return(null).Repeat.Once();
To ensure that you're specifying the number of times that things are to be repeated. (Don't have Visual Studio to hand, syntax may not be exact.)

Method 'Boolean Contains(System.String)' has no supported translation to SQL

"Method 'Boolean Contains(System.String)' has no supported translation to SQL."
query is IsQueryable but this stopped working:
foreach (string s in collection1)
{
if (s.Length > 0)
{
query = query.Where(m => m.collection2.Contains(s));
}
}
UPDATE: it works when i make query "ienumerable" instead of iqueryable. What would be the way to get same result using linq instead of iterating through loop?
Try this:
query = query.Where(m => m.collection2.ToList().Contains(s));
^^^^^^^^
Take a look at this answer from stackoverflow.
It looks like the resulting query would need access to something that the database
has no way of reaching, because the info is in memory.
Since m.collection2 is in the database, don't use Contains. Use Any
m.collection2.Any(x => x == s)
It looks like the error you are seeing is coming from the collection collection 2. Have you tried wrappering the m.collection2 in another function which returns true or false? Is this LINQ syntax?