I want get a list of ids in odoo 9...
No working because expected a singleton
order_recs = order_obj.search([('date_order','<=',previous_start_date),('date_order','>=',previous_new_rec_date)]).id
order_line_recs = order_line_obj.search([('order_id','in',order_recs)])
I expected order_line_recs with a list o order_line
Instead of the id attribute, you should use ids. Example:
order_recs = order_obj.search([('date_order','<=',previous_start_date),('date_order','>=',previous_new_rec_date)]).ids
Which will return the list of ids from the RecordSet.
Related
I want to get a collection, group it by 1 field, and order it by multiple fields.
Here are things I've tried:
$permission_groups = collect(Permission::orderBy('group', 'asc')->orderBy('name', 'asc')->get());
$permission_groups->groupBy('group');
Causes Trying to get property of non-object
$permission_groups = Permission::all()->sortBy('group', 'asc')->sortBy('name', 'asc')->groupBy('group');
Causes the collection to be sorted and then resorted.
How do I simply order by 2 columns and group it?
Figured it out:
$permission_groups = Permission::orderBy('group', 'asc')->orderBy('name', 'asc')->get()->groupBy('group');
I have a legacy database that has data elements stored as a comma delimited list in a single database field. (I didn't design that, I'm just stuck with it.)
I have a list of strings that I would like to match to any of the individual values in the "array" in the DB field and am not sure how to do this in Linq.
My list:
List<string> items= new List<string>();
items.Add("Item1");
items.Add("Item2");
The DB field "Products" would contain data something like:
"Item1,Item3,Item4"
"Item3,Item5,Item6"
"Item2,Item7,Item6"
"Item1,Item2"
"Item1"
My first pass at the Linq query was:
var results = (from o in Order
.Where(p=> items.Contains(p.Products)
But I know that won't work. because it will only return the records that contain only "Item1" or "Item2". So with the example data above it would return 0 records. I need to have it return two records.
Any suggestions?
There is a simple clever trick for searching comma-separated lists. First, add an extra , to the beginning and end of the target value (the product list), and the search value. Then search for that exact string. So for example, you would search ,Item1,Item3,Item4, for ,Item1,. The purpose of this is to prevent false positives, i.e., Item12,Item3 finding a match for Item1, while allowing items at the beginning/end of the list to be properly found.
Then, you can use the LINQ .Any method to check that any item in your list is a match to the product list, like the following:
var results = (from o in Order
.Where(o => items.Any(i => (","+o.Products+",").Contains(","+i+",")))
One way would be to parse the list in the Products field:
var results = (from o in Order
.Where(o => items.Any(i => o.Products.Split(',').Contains(i))
But that would parse the string multiple times for each record. You could try pulling back ALL of the records, parsing each record once, then doing the comparison:
var results = from o in Order
let prods = o.Products.Split(',')
where items.Any(i => prods.Contains(i))
select o;
I have ActiveRecord collection and a I want to check my ID in it.
#items_user_id = Item.select("DISTINCT user_id").where(:user_id => current_user.id)
and I check
#items_user_id.include?(params[:id])
returned nil
What's wrong?
UPD 1 : What I need - param[:id] contains id of Item. The user can edit the items created by them only. And I want to check whether the item identifier in the identifier pool items user.
In short: you are comparing Apples to Oranges.
Long Answer:
#items_user_id = Item.select("DISTINCT user_id").where(:user_id => current_user.id) this line will return Array of Item elements.
params[:id] would possibly be String or Integer so it would not be found in the Array.
The select you do executes this query:
SELECT DISTINCT user_id FROM items WHERE user_id = ?
That will yield either nothing, or a single row with just the user_id.
What do you really want?
Try to collect the ids from the array obtained and then check..
#items_user_id.map(&:id).include?(params[:id])
Make sure the datatype of ids(string) matches with params[:id]
I already have a variable containing some groups. I generated that using the following LINQ query:
var historyGroups = from payee in list
group payee by payee.Payee.Name into groups
orderby groups.Key
select new {PayeeName = groups.Key, List = groups };
Now my historyGroups variable can contain many groups. Each of those groups has a key which is a string and Results View is sorted according to that. Now inside each of those groups there is a List corresponding to the key. Inside that List there are elements and each one those element is an object of a particular type. One of it's fields is of type System.DateTime. I want to sort this internal List by date.
Can anyone help with this? May be modify the above query or a new query on variable historyGroups.
Thanks
It is not clear to me what you want to sort on (the payee type definition is missing as well)
var historyGroups = from payee in list
group payee by payee.Payee.Name into groups
orderby groups.Key
select new {
PayeeName = groups.Key,
List = groups.OrderBy(payee2 => payee2.SomeDateTimeField)
};
Is most straightforward.
If you really want to sort only by date (and not time), use SomeDateTimeField.Date.
Inside that List there are elements and each one those element is an object of a particular type. One of it's fields is of type System.DateTime
This leads me to maybe(?) suspect
List = groups.OrderBy(payee2 => payee2.ParticularTypedElement.DateTimeField)
Or perhaps even
List = groups.OrderBy(payee2 => payee2.ObjectsOfParticularType
.OfType<DateTime>()
.FirstOrDefault()
)
I hope next time you can clarfy the question a bit better, so we don't have to guess that much (and come up with a confusing answer)
How would I modify the following code:
var result = from p in Cache.Model.Products
from f in p.Flavours
where f.FlavourID == "012541-5-5-5-651"
select p;
So that f.FlavourID is supplied a range of ID's as a supposed to just one value as shown in the above example?
Given the following ERD Model:
Products* => ProdCombinations <= *Flavours
ProdCombinations is a junction/link table and simply has one composite key in there.
Of the top of my head
string [] ids = new[]{"012541-5-5-5-651", "012541-5-5-5-652", "012541-5-5-5-653"};
var result = from p in Cache.Model.Products
from f in p.Flavours
where ids.Contains(f.FlavourID)
select p;
There are some limitations, but an array of ids has worked for me before. I've only actually tried with SQL Server backend, and my IDs were integers.
As I understand it, Linq needs to translate your query into SQL, and it's only possible sometimes. For example it's not possible with IEnumerable<SomeClass>, which produces a runtime error, but possible with a collection of simple types.