Select in Data dictionary - metadata

i want get all table names where created person is not "-AOS-" (Dynamics AX). In this code i get only who created first line in table:
for (tablecounter=1; tablecounter<=dict.tableCnt(); tablecounter++)
{
tableId = dict.tableCnt2Id(tablecounter);
common = new DictTable(tableId).makeRecord();
select common where common.createdBy !="";
if(common)
{
info(strFmt('%1---%2',common.createdBy,dict.tableName(common.TableId)));
}
}

You can try with scan over SysModelElement and SysModelElementData tables.
SysModelElement me;
SysModelElementData med;
while select firstOnly10 me
where me.ElementType == UtilElementType::Table
exists join med
where med.ModelElement == me.RecId
&& med.createdBy != '-AOS-'
{
info(me.Name);
}

You could also use the project filter (probably not as fast as a direct SQL query, but depending on your requirements more actionable).

Related

Axapta: "The record has never been selected" error after Group By clause

I'm new in Ax 2009.
I would like to update the field of a record in table A after joining with another table B on which I want to apply Group by on its ID.
I tried to run the following code but it throw me an error "Cannot edit a record in tableA. The record has never been selected":
where select forupdate tableA group by tabAid, Dimension[5]
join tableB group by tabBid, Dimension[5]
where tableA.tabAid == tableB.tabBid && tableB.Dimension[5] != "" && tableA.Dimension[5]
{
if(tableA.Dimension[5] != lines.Dimension[5])
{
ttsbegin;
tableA.Dimension[5] = tableB.Dimension[5];
tableA.update()
ttscommit;
}
}
I think it should be caused by the usage of Group By clause on tableA but if i remove it, on the if check, the tableA.Dimension[5] is empty.
Please someone help me.
Thank you.
Yes, the problems are the Group by clauses. Try something like this.
ttsbegin;
while select forupdate tableA where tableA.Dimension[5] != ""
join tableB where tableB.tabBid == tableA.tabAid && tableB.Dimension[5] != ""
{
if(tableA.Dimension[5] != tableB.Dimension[5])
{
tableA.Dimension[5] = tableB.Dimension[5];
tableA.update()
}
}
ttscommit;
*I made the code without compiler, maybe it needs some adjustment.

How to extract the Rationale and Problem applied to a Requirement artifact in Enterprise Architect through API

I have a few requirements defined in the Requirement Diagram as shown in the below picture. I want to fetch the Rationale and Problem that are attached to the Requirement through API.
The DB has no relationship between the Requirement and Note in the t_object table. Do I have to look up in some other table? Please suggest.
EA.Elements(t_object) are connected with each other using EA.Connector (t_connector)
So in order to get from your requirement to your note, you can do something like this
foreach (EA.Connector connector in myRequirement.Connectors)
{
EA.Element otherElement;
if (connector.ClientID != myRequirement.ElementID)
{
otherElement = myRepository.GetElementByID(connector.ClientID)
}
else
{
otherElement = myRepository.GetElementByID(connector.ClientID)
}
if (otherElement.Type == "Note")
{
return otherElement;
}
}
In a query you can something like this
select note.*
from t_object o
inner join t_connector c on o.Object_ID in (c.Start_Object_ID, c.End_Object_ID)
inner join t_object note on note.Object_ID in (c.Start_Object_ID, c.End_Object_ID)
and note.Object_Type = 'Note'
where o.ea_guid = '<myRequirementGUID>'

How to Data Fetch using Entity Framework in dotnet core

I have a table called "UserAnswers".below screenshot contains table data
I want to get data by surveyId and group by CreatedBy column.
for an example
There is a user called "amara#gmail.com".this user contains 4 records for a SurveyId.
I want to get this like below
Answers : [
{"2"},
{"1","0","1","1"},
{"1","2","4","3"},
{"Blue"}]
But my code returns this array for every rows.I meant duplicate records returning.
Here is my code
var qstns = await (from uans in _context.UserAnswers
where uans.SurveyId == id
select new UserAnswersReturnDto
{
UserEmail = uans.CreatedBy,
Qustns = (from ans in _context.UserAnswers
where ans.CreatedBy == uans.CreatedBy
select new UserAnswersSet
{
QNo = ans.QNo,
Ansrs = JsonConvert.DeserializeObject<JArray>(string.IsNullOrEmpty(ans.Answers) ? "[]" : ans.Answers)
}).ToArray()
}).ToListAsync();
So how to solve this issue.I opened many questions for this problem,but no one answered.Please help me.Thanks in advanced
You need to actually group your data before returning:
I used LINQ Lambda notation, but it should be quite easy to translate back to query if you're so inclined:
var qstns = _context.UserAnswers.Where(uans => uans.SurveyId == id)
.GroupBy(uans => uans.CreatedBy)
.Select(grans => new UserAnswersReturnDto {
UserEmail = grans.Key,
Qustions = grans.Select(ans => new UserAnswersSet() {
QNo = ans.QNo,
Ansrs = ans.Answers
}).ToList()
} ).ToList();
I didn't have time to double-check this, but I hope it serves as a guide to help you solve your issue!
There is no group by statement in your linq query.

EF Left joining a table on two properties combined with a case statement

I'm trying to write a query for a database that will left join a table to a look up table and the results will be returned based on a case statement.
In normal SQL the query would look like this:
SELECT chis_id, chis_detail, cilt.mhcatID, cilt.mhtID, 'TheFileName' =
CASE
WHEN cilt.mhcatID IS NOT NULL AND cilt.mhtID IS NOT NULL THEN chis_linked_filename
END
FROM chis
LEFT JOIN cilt on cilt.mhcatID = chis.mhcat_id AND cilt.mhtID = chis.mht_id
WHERE cch_id = 50
chis is the table being queried, cilt is a look-up table and does not contain any foreign key relationships to chis as a result (chis has existing FK's to mht and mhcat tables by the mhtID and mhcatID respectively).
The query will be used to return a list of history updates for a record. If the join to the cilt lookup table is successful this means that the caller of the query will have permission to view the filename of any associated files for the history updates.
Whilst during my research I've found various posts on here relating on how to do case statements and left joins in Linq to Entity queries, I've not been able to work out how to join on two different fields. Is this possible?
You need to join on an anonymous type with matching field names like so:
var query = from x in context.Table1
join y in context.Table2
on new { x.Field1, x.Field2 } equals new { y.Field1, y.Field2 }
select {...};
A full working example using the an extra from instead of a join would look something like this:
var query = from chis in context.Chis
from clit in context.Clit
.Where(x => x.mhcatID = chis.mhcat_id)
.Where(x => x.mhtID = chis.mht_id)
.DefaultIfEmpty()
select new
{
chis.id,
chis.detail,
cilt.mhcatID,
cilt.mhtID,
TheFileName = (cilt.mhcatID != null && cilt.mhtID != null) ? chis.linked_filename : null
};
Based on what Aducci suggested, I used a group join and DefaultIsEmpty() to get the results I wanted. For some reason, I couldn't get DefaultIfEmpty() didn't work correctly on its own and the resulting SQL employed an inner join instead of a left.
Here's the final code I used to get the left join working:
var query = (from chis in context.chis
join cilt in context.cilts on new { MHT = chis.mht_id, MHTCAT = chis.mhcat_id } equals new { MHT = cilt.mhtID, MHTCAT = cilt.mhcatID } into tempCilts
from tempCilt in tempCilts.DefaultIfEmpty()
where chis.cch_id == 50
select new {
chisID = chis.chis_id,
detail = chis.chis_detail,
filename = chis.chis_linked_filename,
TheFileName = (tempCilt.mhcatID != null && tempCilt.mhtID != null ? chis.chis_linked_filename : null),
mhtID = chis.mht_id,
mhtcatID = chis.mhcat_id
}).ToList();

Using ObjectQuery Include and using a nested where clause

Using entity framework, I'm trying to get back a customer with order details but I want to filter out those Orders that are active.
Customer is our EntityObject which has a collection of Order EntityObjects. CustomerDetails is our ObjectContext.
The code below will attach all orders but I want to filter and only attach those that are active. (Order.active == true). How can I do this?
I know Include builds up a nested query statement (I can observe by using .ToTraceString().) I was hoping to attach a Where clause to this nested select statement or the Include.
Customer cust;
CustomerDetails custTable = new CustomerDetails();
cust = custTable.Customer
.Where("it.cust_id = " + id)
.Include("Order") // But we only want Order.active == true!!!
.ToList().First();
Untested, but might work?
var temp = custTable.Customer.Where("it.cust_id = " + id).Include("Order");
cust = (from t in temp
where t.Order.active == true
select t).ToList().First();