return parent child data through sql - parent-child

I am in a situation where I want to return a list of article objects. The properties of Article object looks as follows.
private int _articleId;
private string _articleName;
private List<Tag> _tags;
Now as you can see the article object has a property called _tags which is a list of type "Tag". Now assuming that I want to return a list of Article objects, how can i populate this list of type Tags when I am returning the list of Article objects.
I am looking to display the result as follows:
ArticleName
Tag1 Tag2 Tag3

This is the sort of thing I would employ Hibernate or NHibernate for.
This answer actually provides a great leg up.

Get the list of Articles.
Then use a SQL query to get the list of Tags (assuming the resulting data has some sort of identifier as to what article it applies to.
Loop over these tags and build the lists for the correct Articles accordingly.

Related

Problem with one controller handling two forms (models)

I have to implement this form, which at first seemed easy to me. For the models part, I created a one-to-many relationship between GeneralInformation and CourseList (very obvious). In the GeneralInformation I included the bottom section as well with the 'Comments', 'Remarks'etc.
The problem is that before submitting the General Information section, you fill the Course List which will give an error for the FK constraint (again, obvious). For the Course List I'm using DevExtreme datagrid.
The only solution I came up with is to create another table, which keeps the ID of GeneralInformation and each Course ID. A similar solution for many-to-many relationships. If this seems like a viable solution, then how do you store the IDs of Courses in the controller, and then the ID of GeneralInformation, to put them in the database. Now I think I'm handling two models with the same controller, which might not be an optimal solution or even against guidelines of .NET Core.
If someone has a better solution, it would be greatly appreciated.
Your Model can be like this
public class Model_Name //enter your model name
{
public string FirstName {get;set;}
public string LastName {get;set;}
//other properties from General Information form
public List<Course> Courses {get;set;} //Course is a separate model
public string Remarks {get;set;}
}
In HTML do not map course to General info, Post the form and pass all the fields as json like below
{'FirstName':'Test','LastName':'Test','Courses':[{'Id':1}], 'Remarks':'test'}
So you're basically asking how to bind to a collection in a model.
Property binding would look like this:
<input asp-for="#Model.Collection[i].Property" />
What i did was store the course table (or grid) row as a partial view that takes an index as an argument with the course model and at the same time i maintain an index of the courses count using data-id attribute on each row and then every time a user wants to add a course i use ajax with the index of the last course as a parameter to fetch a new row rendered with an incremented index
and i append it to the table.
That way when your submit your form it will bind to a collection what ever the length is.

Application Insights - How to search by some properties in custom dimension

I am using mobile application i.e Appcenter for recording customevents AzureLogportal.
I am trying to fetch the records by using custom query.
Inside customDimensions Object of array i.e Properties i stored.
Properties = {"1":"Studentname","101":"id","John":"Title"}
I am trying to fetch all the records for title : John in the properties.
Query i tried is :
customEvents
|where customDimensions.Properties.TopicTitle == "John"
But it dont give any result.Please help.
if Properties within customDimensions is a property bag, then the bottom note in this document should have the answer
https://learn.microsoft.com/en-us/azure/kusto/query/parsejsonfunction
Notes
It is somewhat common to have a JSON string describing a property bag in which one of the "slots" is another JSON string. For example:
let d='{"a":123, "b":"{\\"c\\":456}"}';
print d
In such cases, it is not only necessary to invoke parse_json twice, but also to make sure that in the second call, tostring will be used. Otherwise, the second call to parse_json will simply pass-on the input to the output as-is, because its declared type is dynamic:
let d='{"a":123, "b":"{\\"c\\":456}"}';
print d_b_c=parse_json(tostring(parse_json(d).b)).c

Firestore: How to query data from a map of an array

Here is the schema for test db:
I want to write a query that can get all the documents where contacts have id=1 in any of the array index.
I have checked array_contains operator for firestore but the thing is my array have map which then have field id.
Thanks
You currently can't build a query that looks for an object field within an array. The only things you can find in an array are the entire contents of an array element, not a part of an element.
With NoSQL type databases, the usual practice is to structure you data in a way that suits your queries. So, in your case, you're going to have to structure your data to let you find documents where an array item contains a certain string. So, you could create another array that contains just the strings you want to query for. You will have make sure to keep these arrays up to date.
You could also reconsider the use of an array here. Arrays are good for positional data, but unless these contacts need to be stored in a certain order, you might want to instead store an object whose keys are the id, and whose field data also contains the id and name.
As Doug said, you can't query it,
but, if you could structure your data to something that looks like this
Store your data as a map
Use id as key and name as value
Now you can write a query that can get all the documents where contacts have id=1
db.collection("test").where("contacts.1", ">=", "").get()
If you have an array of maps or objects and want to get the docoments that contains this specific map/object like this ?
array_of_maps.png
you can use
queryForCategory(categName: string, categAlias: string): Observable[] {
firestore.collection('<Collection name>', ref =>
ref.where('categories',
"array-contains", {
"alias": categAlias,
"title": categName
}
One soulions is as #Doug said, to add array with the data you need to query, and keep it uptodate.
Another solution is to make contacts as sub collection of the document, an then you could make queries of the contacts and get its parrent.
final Query contacts = db.collectionGroup("contacts").whereEqualTo("id", 1);
for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
System.out.println(document.getId());
System.out.println(document.getString("parent_id"));
}
If you don't want to add parent as another field, you can get it from parent reference, check this answer https://stackoverflow.com/a/56223319/1278463
snapshot.getRef().getParent().getParent().collection("people")
https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query

Linq to SQL Design question

Often I need to combine data from multiple tables and display the result in a GridView control.
I can write a Linq query inline in the Page_load event, return an anonymous type that combines all the fields that I need, and databind the result to the GridView control.
Problem: I use 'helper methods' as described by Scott Guthrie on his blog. Such a helper method cannot return an anonymous type. The query would have to be inline for this approach.
I can write a database view that returns the data that I need, and write a helper method with a query against this (new and known) type that it returns.
Problem: I will need a lot of views in my database schema, and I will introduce a lot of redundant aspects of my data. I also lose some of the advantage of using Linq - removing all business logic from the database.
I would like to take an approach that lets me keep the Linq queries in helper methods, yet allows me to access all the attributes that I need on the grid in their respective databinding expressions. Can this be done?
I asked the wrong question, as I frequently do. What prompted me to look into anonymous types was an apparent limitation of the GridView - my inability to use a databinding expression in an <asp:BoundField> (the DataField parameter only accepts column names of the table that the Linq query pulls in).
Turns out that in a TemplateField it is possible to use Eval and access members of the Linq data item, and Linq takes care of the query for me.
In other words, I can keep the query in my helper method, have it return a primary database table type (e.g. Account), and I bind the Accounts to the GridView.
In the databinding expressions I can access data members of the Account objects that reside in other tables, without having to explicitly pull them in in the query. Perfect.
I don't know if there is a viable way to achieve this using anonymous types. But I have a suggestion that will work in WinForms, but I am not sure about ASP.NET.
What you need is a type with properties where neither the number of properties, nor the types and names of the properties are known at compile time. One way to create such a thing is ICustomTypeDescriptor.
You have to create a type implementing this interface with an private backing store of objects backing the properties returned by the query for one row from the query. Then you implement GetProperties() to return one PropertyDescriptor per column and PropertyDescriptor.GetValue() and PropertyDescriptor.SetValue() to access the backing array.
By implementing PropertyDescriptor.Name you will get the correct column name; this will probably require another backing store storing the property names. And there is a lot more to implement, but in the end your new type will behave almost like a normal type - and now the if - if the control you are binding to knows about and uses ICustomTypeDescriptor.
UPDATE
I just found an bit of text stating that ASP.NET data binding knows and uses ICustomTypeDescriptor.
Scott's earlier post in the series talks about shaping the result set before inserting into a grid:
Part 3 - Querying our Database
Scroll down to "Shaping our Query Results".

How do you avoid the n+1 problem with SubSonic?

Ayende has a blog post detailing how to combat the "n+1" problem in nHibernate. In essence, the problem is this:
Assume you have two tables, "BlogPosts" and "Comments" with a one-to-many relationship between them (i.e. each BlogPost can have multiple Comments). Now, let's say you want to execute the following nested for loop:
foreach (BlogPost post in blogposts)
{
foreach (Comment comment in post.Comments)
{
// Do something
}
}
From what I understand, the classes that SubSonic generate are lazyload by default (I don't want to turn this off completely because it's useful in most circumstances, just not this one). That means that every time the inner loop is executed (i.e. when post.Comments is accessed), a separate query must be sent to the database to retrieve the comments.
So if you have 80 blog posts, that's 1 query to get the list of blog posts, then 80 queries to get the comments for each of them. If the comments were eager loaded, however, this would be reduced to 1 query.
Is there any way to combat this problem in SubSonic currently?
Unless you create a query to select from Comment based on post IDs, I don't think there's a way to combat it. This would get you down to two. One query to select the post IDs you want, then another to get all the comments for that list of post IDs.
I think what you should do is create a partial class method that will get all the comments. Not so clean but should work.
I too use partial classes and load related table classes like so:
Partial Public Class Book
Private _Author as Database.Author
Property Author() as Database.Author
Get
If _Author is nothing then
' Load the author class here.
End if
return _Author
End get
Set
'....
End Set
End Property
End Class

Resources