I want to import all products filtering these they applied all 3 criteria:
1) they are in stock (DIM)
2) stock is more than 3 pcs (stock_indicator)
3) and they belong to one (any) of these groups 1 or 4
I want all 3 criteria, but in 3rd any of these options
i.e.:
/product[dim1[1] = "1" and stock_indicator[1] > 3 and group[1] = "1" or group/category/id[1] = "4"]
The above does not returns any product, like no product have all these requirements.
What am I doing wrong?
Dim = availability
XML sample:
/product[dim1[1] = "1" and stock_indicator[1] > 3 and group[1] = "1" or group/category/id[1] = "4"]
First of all your XPath assumes that all product elements are at root level, which would not make for a well-formed XML document; the all should be wrapped in some element.
If that is no problem in your environment (since we do not know the whole setup from your question) probably the most prominent problem in your XPath is that you try to compare the value of stock_indicator against a xs:integer but in fact your data sample encodes them as xs:string.
Consequently
stock_indicator[1] > 3
will always return false…
Try
stock_indicator[1]/number() > 3
or
number(stock_indicator[1]) > 3
instead.
Nevertheless depending on the data structure (e.g. multiple stock_indicatorelements in one product [whatever that might mean]) this could return false positives.
You can use the following XPath to filter the products :
//product[availability="1"][stock_indicator>3][group/id=1 or group/id=4]
// at the beginning as stated by #Benjamin W. Bohl to catch all products
availability is used instead of "Dim"
cleaner syntax used for predicates
no position indexes used ([1]) assuming you only have 1 availability, 1 stock_indicator, 1 id per group in each product
XML used to test.
XPath :
XML is filtered (2 of the 4 products fulfill the conditions) :
Related
I have the below requirement to be implemented in a plugin code on an Entity say 'Entity A'-
Below is the data in 'Entity A'
Record 1 with field values
Price = 100
Quantity = 4
Record 2 with field values
Price = 200
Quantity = 2
I need to do 2 things
Add the values of the fields and update it in a new record
Store the Addition Formula in a different config entity
Example shown below -
Record 3
Price
Price Value = 300
Formula Value = 100 + 200
Quantity
Quantity Value = 6
Formula Value = 4 + 2
Entity A has a button named "Perform Addition" and once clicked this will trigger the plugin code.
Below is the code that i have tried -
AttributeList is the list of fields i need to perform sum on. All fields are decimal
Entity EntityA = new EntityA();
EntityA.Id = new Guid({"Guid String"});
var sourceEntityDataList = service.RetrieveMultiple(new FetchExpression(fetchXml)).Entities;
foreach (var value in AttributeList)
{
EntityA[value]= sourceEntityDataList.Sum(e => e.Contains(value) ? e.GetAttributeValue<Decimal>(value) : 0);
}
service.Update(EntityA);
I would like to know if there is a way through linq I can store the formula without looping?
and if not how can I achieve this?
Any help would be appreciated.
Here are some thoughts:
It's interesting that you're calculating values from multiple records and populating the result onto a sibling record rather than a parent record. This is different than a typical "rollup" calculation.
Dynamics uses the SQL sequential GUID generator to generate its ids. If you're generating GUIDs outside of Dynamics, you might want to look into leveraging the same logic.
Here's an example of how you might refactor your code with LINQ:
var target = new Entity("entitya", new Guid("guid"));
var entities = service.RetrieveMultiple(new FetchExpression(fetchXml)).Entities.ToList();
attributes.ForEach(a => target[a] = entities.Sum(e => e.GetAttributeValue<Decimal>(a));
service.Update(target);
The GetAttributeValue<Decimal>() method defaults to 0, so we can skip the Contains call.
As far as storing the formula on a config entities goes, if you're looking for the capability to store and use any formula, you'll need a full expression parser, along the lines of this calculator example.
Whether you'll be able to do the Reflection required in a sandboxed plugin is another question.
If, however, you have a few set formulas, you can code them all into the plugin and determine which to use at runtime based on the entities' properties and/or config data.
I’m using Redmine and Computed Custom Field plugin.
The plugin provides a possibility to make custom fields computed and it accepts ruby code for calculations.
In Redmine I have a project (Project_id = 11) where in I calculate the cost of products in a separate custom field for each issue. It looks like this:
Each Issue has a custom field (cf_id = 31) for selecting the product: Pears, Pineapples, Tomatoes, Coconuts.
Each Issue has a custom field (cf_id = 32) for entering the quantity (pieces) of goods.
Each Issue has a custom field (cf_id = 33) for entering the weight (pounds) of goods.
Each Issue has a computed custom field (cf_id = 34) in which the formula calculates the cost of the product.
The formula in the computed custom field (cf_id = 34) includes two hashes with prices of products (depending on the product type):
products_by_weight = {
"Pears" => [110],
"Tomatoes" => [120]
}
products_by_pieces = {
"Pineapples" => [130,300],
"Coconuts" => [140,200]
}
Then my formula checks the product selected in cf_id = 31 for belonging to the first or the second hashes and performs the corresponding calculations:
Multiplies the price by weight (cf_id = 32) in case of using the goods from the first list
Or multiplies the price by the quantity (cf_id = 33) in case of using the goods from the second list. The second value in the value array of "products_by_pieces" hash is the weight limit per piece. If the weight divided by the limit is a larger amount than entered in cf_id = 32, then the formula in scenario 2 will use this quantity instead of the one indicated in cf_id = 32.
Now I`m trying to move these variables outside the formula. I made a project (Project_id = 22) in which I want to save these variables as issues.
I imagine it like this:
The name of the issue is the name of the product
Each issue has two custom fields:
cf_id = 41 is price of product
cf_id = 42 is weight limit per piece
For each issue a category is assigned: "products_by_weight" or "products_by_pieces".
I want to compile the same hashes that are now included in my formula in the cf_id = 34 of the issues of the project 11, but automatically from the issues of the project 22, taking into account the category.
So far, all I have achieved is to find the price of a known product from such issues of the project 22.
price = Project.find(22).issues.where(subject: "Pineapples").first.try(:custom_field_value,41)
But this does not help in any way and requires changes to the code when adding each new product.
I'm new to programming and Ruby, so I’m trying to experiment with Redmine classes, and tried to compile a hash with such code:
Issue.by_category(Project.find(22))
But as a result, so far I have received only this:
[{"status_id"=>"27", "closed"=>true, "category_id"=>"1", "total"=>"10"}]
Which is completely different from the result I expect.
Any help would be helpful!
UPD.
Right now, my variables (product prices and weight limits) are in a hash, which is directly a part of the code for computed field 34. But I do not want these variables (prices and weight limits) to be part of the code. I want to manage them as Issues with corresponding custom fields (41 and 42) in a separate project (22) - in such a way that regular user can change or add these values in Issues without having to change the code of the calculated custom field (34). So I want to compile that hash based on the Issues from Project 22 instead of writing it directly. I assume this is so, that the Subjects of the Issues of the Project 22 should become the keys and array of custom fields [41,41] - the values. In doing so, I need two separate hashes determined by the assigned category ("goods_by_weight" and "goods_by_pieces") because they are calculated differently and in Project 22 I have other variables written as values of custom fields in Issues with a different category.
I solved this problem in the following way.
As planned, now I store the price list for my products as issues in a separate Project (ID 22). To get the prices hash of all products of the selected category (A) in the computed custom field's formula if Issue of Project (ID 11), I do the following:
PRICELIST_PROJECT_ID = 22
CATEGORY_A_ID = 1
PRICE_VALUE_CFID = 41
WLIMIT_VALUE_CFID = 42
delimiter = ','
pricelist_issues_cat_a = Project.find(PRICELIST_PROJECT_ID).issues.select { |rate| rate.category_id == CATEGORY_A_ID }
cata_products_names = []
cata_products_pvalues = []
for i in (0..pricelist_issues_cat_a.size-1) do
cata_products_names[i] = pricelist_issues_cat_a[i].try(:subject)
cata_products_pvalues[i] = pricelist_issues_cat_a[i].try(:custom_field_value,PRICE_VALUE_CFID).split(Regexp.union(delimiter)).map(&:to_f)
end
cata_price_hash = Hash[cata_products_names.zip(cata_products_pvalues)]
And the same way for product category B.
Not sure if this is the most efficient way, but it works for me.
I used "Union" to combine two different subject area in an analysis in OBIEE 11 :
"A" is a column in the first subject area with formula that needs
"A_Dim" to be calculated using "A_Dim" and Case-When (So I Should Use "A_Dim" in first subject area then exclude it in result)
"A" equals to zero in second subject area
"B" is a column in the second subject area
"B" equals to zero in first subject area
"C" is a column in Result (Using Add Result Column) that has this formula :
SUM("A" BY sth ) / SUM("B" BY sth)
("A","B",... replaced with saw_i in result column formula as you know)
the problem is, I can not get top 10 rows ordering by "C" ??
(I tried using RANK, TOPN , TOPN(RANK()),... with no luck)
(and one more thing, there are two problem with using "Narrative view" instead of other views , first they want a bar chart, besides in narrative there is no Exclude option and I should use javaScript to get top 10 from thousands of repeated "C" values)
The original question is old, but I figured I would add my solution in case anyone ends up here like I did.
I was able to create a "Result Column" with my TOPN formula. I did not have to create equivalent columns in the two queries that are being unioned (aka there are 4 columns in each union query but 5 columns in the "Result Columns" overall). It seems to work as expected.
I need a little help with Linq i have a DB table with restourants tables in it. In the DB i have "TableNumber, Floor , RestaurantID" I would like to get list of all floors. For example if i have this list:
TableNumber, Floor , RestaurantID
10 1 1
11 1 1
12 2 1
13 2 1
14 3 1
I would like to get only "1,2,3".
Right now the method returns all rows.
public IEnumerable<ListPad.Item> GetFloorsListPads(SSF.ART.Key restaurant_id)
{
return from restaurant_floor in EnterpriseTouchRestaurantApplication.TouchRestaurant.AllRestaurantTables
where restaurant_floor.RestaurantID == restaurant_id && restaurant_floor.Active == true
orderby restaurant_floor.Floor
select new ListPad.Item()
{
Color = Color.SkyBlue,
Text = string.Format("{0}", restaurant_floor.Floor),
Tag = restaurant_floor
};
}
Thanks for all the help in advance.
You need either one or two things, depending of whether or not ListPad.Item defines equality (by overriding Equals and GetHashCode) in the way you describe. If so, then adding .Distinct() to your query will give you the distinct items. If not, you can do it one of three ways.
Return an anonymous type, call Distinct on it, and map to the actual type (lazy way)
Implement IEquatable on ListPad.Item, overriding Equals and GetHashCode (you'll need to research how to properly implement GetHashCode so it matches your equality conditions)
Define an IEqualityComparer<ListPad.Item> that defines your equality.
1 is the lazy way but is less coding. 2 is handy if your conditions define equality for ALL uses of ListPad.Item (not just this particular scenario). 3 separates the equality check from the actual type, which is handy if you have other cases where equality would be defined differently.
I have a database table that holds parent and child records much like a Categories table. The ParentID field of this table holds the ID of that record's parent record...
My table columns are: SectionID, Title, Number, ParentID, Active
I only plan to allow my parent to child relationship go two levels deep. So I have a section and a sub section and that it.
I need to output this data into my MVC view page in an outline fashion like so...
Section 1
Sub-Section 1 of 1
Sub-Section 2 of 1
Sub-Section 3 of 1
Section 2
Sub-Section 1 of 2
Sub-Section 2 of 2
Sub-Section 3 of 2
Section 3
I am using Entity Framework 4.0 and MVC 2.0 and have never tried something like this with LINQ. I have a FK set up on the section table mapping the ParentID back to the SectionID hoping EF would create a complex "Section" type with the Sub-Sections as a property of type list of Sections but maybe I did not set things up correctly.
So I am guessing I can still get the end result using a LINQ query. Can someone point me to some sample code that could provide a solution or possibly a hint in the right direction?
Update:
I was able to straighten out my EDMX so that I can get the sub-sections for each section as a property of type list, but now I realize I need to sort the related entities.
var sections = from section in dataContext.Sections
where section.Active == true && section.ParentID == 0
orderby section.Number
select new Section
{
SectionID = section.SectionID,
Title = section.Title,
Number = section.Number,
ParentID = section.ParentID,
Timestamp = section.Timestamp,
Active = section.Active,
Children = section.Children.OrderBy(c => c.Number)
};
produces the following error.
Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'System.Data.Objects.DataClasses.EntityCollection'
Your model has two navigation properties Sections1 and Section1. Rename the first one to Children and the second one to Parent.
Depending on whether you have a root Section or perhaps have each top-level section parented to itself (or instead make parent nullable?), your query might look something like:-
// assume top sections are ones where parent == self
var topSections = context.Sections.Where(section => section.ParentId == SectionId);
// now put them in order (might have multiple orderings depending on input, pick one)
topSections = topSections.OrderBy(section => section.Title);
// now get the children in order using an anonymous type for the projection
var result = topSections.Select(section => new {top = section, children = section.Children.OrderBy(child => child.Title)});
For some linq examples:
http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
This covers pretty much all of the linq operations, have a look in particular at GroupBy. The key is to understand the input and output of each piece in order to orchestrate several in series and there is no shortcut but to learn what they do so you know what's at hand. Linq expressions are just combinations of these operations with some syntactic sugar.