Is there a way to retrieve objects of a %Library.RelationshipObject by a certain order - objectscript

This documentation has no mention of sorting the elements

As an example I'll show sql query by class Sample.Employee in SAMPLES namespace, where properties Spouse and Company are relationships. You may get access to related object via -> and you can order by alias.
SELECT
title,
Spouse->Name,
Company->Name as CompanyName
FROM
sample.employee
ORDER BY
CompanyName

Related

Dynamics 365 OData getting 1:N linked entities

I have entities of type account which when I look at it in Dyn365 it has a section containing a list view of related entities. How do i get these related entities from the OData API?
I can query api/data/v9.1/account but the related entities do not appear anywhere in the resulting json.
If I do the same for the related entities the account do not appear anywhere. How do I get the link between these two types of entities? using the OData API.
I've tried things like /accounts?$expand=contact($select=foo) but it just says contact property do not exist on the account entity, which is correct. But contact is not a property its an entity type.
Solution is to use the following url to get the names of all relationships.
https://<company>.crm.dynamics.com/api/data/v9.1/RelationshipDefinitions?$select=SchemaName
Result is an array of the following rows for each relationship.
{"#odata.type":"#Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata","SchemaName":"aaa_bbb","MetadataId":"<guid>"},
Then use those names, specifically the SchemaName property in the $expand query parameter.
https://crmdev.crm.dynamics.com/api/data/v9.1/accounts?
$select=<whatever>&
$expand=aaa_bbb($select=<properties from linked entity>)
To figure out what relationship actually do what you need, you need to read in more then just the SchemaName if you can't guess what is what, or look it up in Dynamics through the GUI if you know how and have access. I don't.
The RelationshipDefinition contains other properties like ReferencedEntity ReferencingEntity that can be used to determine if you got the right relationship.
Probably, this is what you are looking for. The relationship for 1:N between account and contact is contact_customer_accounts
All accounts with its related contacts:
https://crmdev.crm.dynamics.com/api/data/v9.1/accounts?$select=name&$expand=contact_customer_accounts($select=fullname)
Particular account with related contacts:
https://crmdev.crm.dynamics.com/api/data/v9.1/accounts(73C84814-729B-EA11-A811-000D3A370DB6)?$select=name&$expand=contact_customer_accounts($select=fullname)

Runtime join in lightswitch

I have a generic group members table with a GUID for a "group type" and a GUID for "referenced object". An example would be if I have a table of customers(each having a GUID) I can group them under "already paid" by creating a group GUID and in my "Group members table" referencing every customer by their respective GUID. This allows for any type of group to be added to the model as we expand(without adding extra tables).
Here is the problem. I have created a subquery in an entity in order to filter the universal group members table for a certain group and what "items" are and are not in that group; like so:
partial void ElementsNotMemberOfGroup_PreprocessQuery(int? UniversalGroupTypeIDParameter, int? UniversalGroupsIDParameter, ref IQueryable<UniversalGroupMember> query)
{
query = query.Where(x => x.UniversalGroup.UniversalGroupType.UniversalGroupTypeID == UniversalGroupTypeIDParameter);
query = query.Where(x => x.UniversalGroup.UniversalGroupsID != UniversalGroupsIDParameter);
}
This returns the GUIDs for the referenced object in the group, but for a user that's useless. I need to join this table and my customers table at runtime on the GUID so I can extract the customer info and display it.
Any Ideas?
LightSwitch wasn't really created with this kind of scenario in mind. LightSwitch makes things very easy for you when you create relationships between tables that are, well, "related". When you do this, you never need manual joins between entities.
While it's possible to do something similar to what you're describing (see the link below), it's a lot more work to achieve it, and in my opinion it isn't really worth the extra trouble. Not only that, but as you're discovering, it complicates even the most simple operations.
In essence, you're working against LightSwitch, instead of with it. My advice to you would be that if you really must do this type of manual optimization, then LightSwitch may not be the best product for you to use.
Beth Massi has a blog article, Using Different Edit Screens Based on Record Types (Table Inheritance), which isn't exactly what you're doing, but it may give you some ideas if you decide to still use LightSwitch for your project.

Use LINQ for double-nested OData collection

I've got a custom OData feed that for books. Each book can have multiple authors and an author can be involved in multiple books so I implemented this using a join table (Book - BookAuthorJoin - Author). My proxy object has Book.BookAuthorJoins BookAuthorJoin.Books & BookAuthorJoin.Authors.
What I want todo is have a single query where I get all the books for an author in a single LINQ query, but having trouble applying the filter. Seems I want two Expand() methods, but that isn't working. The following query doesn't work, but shows what I'm trying to do:
var query = from book in ODataContext.Books.Expand("BookAuthorJoins").Expand("Authors")
where book.BookAuthorJoins.Author.AuthorID = authorID
select book;
On the server side, the 1-to-many or many-to-many relationship is usually exposed as just a navigation property, exposing the join table in the middle will make your life much harder. If you use EF you should be able to hide the table and just expose the relationship as a navigation property.
In any case, to get all books for a certain author the query should look like:
/Authors(123)/Books
The result of this query is just a feed of books.
If you do keep the join table exposed then something like this migth work:
/Authors(123)/BookAuthorJoins?$expand=Book
But this time you get all the BookAuthorJoins with the Book for each as well.

Can someone explain Query Reshaping in Linq with the following example?

I am reading this asp.net article on building your first asp.net mvc 2 website and I came across a Linq query that uses the Include method in the query. I have used some linq, but I have never used the Include method and would like a better explanation. Does it translate to an join in linq? What is the benefit? Here is the query from the article:
var genreModel = storeDB.Genres
.Include("Albums")
.Single(g => g.Name == genre);
The article states that:
Entity Framework feature that allows us to specify other related entities we want loaded as well, called Query Result Shaping. We want to load the Albums for the matching Genre, so we'll query from Genres.Include("Albums") to indicate that we want related albums as well. This is more efficient, since it will retrieve both our Genre and Album data in a single database request.
I sort of understand what the author is saying above, but feel I would need a better example or explanation, especially since I have never used the Include method before.
If you inspect the generated sql, you'll notice that the Albums table is joined in. It should look something like:
SELECT [t0].*, [t1].*
FROM Genres [t0]
LEFT JOIN Albums [t1] ON [t0].GenreId = [t1].GenreId
WHERE [t0].Name == #p0
When the results get back to the client side, the ObjectContext will turn the row-column shape into instances of Genres and Albums. These instances will be related hierarchically - the single Genre with all of its Albums.
Suppose this genre has 5 albums. The query will return 5 rows. The object context will create one instance of Genre (each of the 5 rows has the same Genre primary key value).

Can I use the auto-generated Linq-to-SQL entity classes in 'disconnected' mode?

Suppose I have an automatically-generated Employee class based on the Employees table in my database.
Now suppose that I want to pass employee data to a ShowAges method that will print out name & age for a list of employees. I'll retrieve the data for a given set of employees via a linq query, which will return me a set of Employee instances. I can then pass the Employee instances to the ShowAges method, which can access the Name & Age fields to get the data it needs.
However, because my Employees table has relationships with various other tables in my database, my Employee class also has a Department field, a Manager field, etc. that provide access to related records in those other tables. If the ShowAges method were to invoke any of those methods, this would cause lots more data to be fetched from the database, on-demand.
I want to be sure that the ShowAges method only uses the data I have already fetched for it, but I really don't want to have to go to the trouble of defining a new class which replicates the Employee class but has fewer methods. (In my real-world scenario, the class would have to be considerably more complex than the Employee class described here; it would have several 'joined' classes that do need to be populated, and others that don't).
Is there a way to 'switch off' or 'disconnect' the Employees instances so that an attempt to access any property or related object that's not already populated will raise an exception?
If not, then I assume that since this must be a common requirement, there might be an already-established pattern for doing this sort of thing?
maybe not the answer you're looking for,but how about projecting the results of your query into a more light-weight POCO, eg:
var employeePOCOs = from e in l2sEmployees
select new EmployeePOCO
{
Id = e.Id,
Name = e.FirstName + " " + e.LastName
};
where EmployeePOCO is a predefined class
would that help? I've used this when returning Entity Framework objects back through an AJAX call where the output was going to JSON, and it seemed to do the trick.
One way to do this is to 'detach' the entity from its database context. Take a look at an answer I gave to a similar question. It shows you a couple ways of detaching entities.

Resources