create generic list object from datatable which has different schema at runtime - list

Datatable should be converted to a generic list. The datatable may have different schema at runtime.so extracting the class type for the generic list becomes difficult. Because as the columns differ at run time, I could'nt create property for the class type accordingly. I want to know how to do that. i am using c#.

Related

Selecting all the Document Types in umbraco

I am using umbraco 7 and have been trying to figure out how to select an array of the distinct document types of a given node's children. I can't hardcode this because I will be needing many different kinds of document types so I was hoping to do a distinct select of them but I can't seem to get my code correct, maybe there is no "distinct" select or maybe I can't just get individual alias strings from the methods I have tried?
Has anyone else attempted this?
Assuming you have an instance of interface type IPublishedContent, you should be able to get the distinct document types of all children as follows:
node.Children.Select(c => c.DocumentTypeAlias).Distinct()

Django ORM join with another table

I have a table known as messages. In my application, users can send different type of messages. Like forwarding an event, etc. As such, I have columns type and value in that table.
What I want to do is for a particular type, goto a particular table and make sure the value is valid (typically this maps to the id of that table). There could be multiple types, and each one has to be mapped to a different table. Is there a way to logically write this in the built in django ORM? Right now I'm only seeing this feasible if I use straight SQL, which I would rather not if I can get away with it...
Right now I'm doing something like:
Messages.objects.all().filter(Q(user_id=id))...etc
I want to add to the statement above checking the type and for the particular type, check the table associated with it.
It sounds like you have a "polymorphic association". There are a couple ways to do analogous things in Django, but I think the one that most closely matches what you described is the contenttypes module, which uses separate columns for the type and for the value as in your application.
You may just want to define a multi-table inheritance structure. This gets you the same result in that you have multiple types of messages and field inheritance, but you don't have to mess with a type field at all.
class Message(models.Model):
value = models.CharField(max_length=9000)
class Event(Message):
pass
class Tweet(Message):
pass
In view post handler:
...
if request.POST['type'] == 'event':
Event.objects.create(value=request.POST['value'])
elif request.POST['type'] == 'tweet':
Tweet.objects.create(value=request.POST['value'])
...
Then, get your objects:
all_tweets = Tweet.objects.all()
all_messages = Message.objects.all()
for message in all_messages:
try:
event = message.event
except Event.DoesNotExist:
# this message is not an Event
pass
If this sort of structure doesn't work for you, there are two other styles of Model inheritance supported by Django: Abstract base classes and Proxy models. You could also hold a GenericForeignKey as suggested by #AndrewGorcester.

JasperReports: Passing in a list of lists as a datasource

I need to populate a few subreports with lists of different objects. Basically lets say i have the following:
Subreport on used Vehicles
Subreport on new Vehicles
I create a vehicle bean class with variables as strings and create getter and setter methods for the same. Then in my datasource I pass in a List<List<String>> as detailRows. detailRows contains a list for new vehicles and a list for used vehicles. So lets say, i pass detailRows in the data source.
Question is how do i pass these two lists to the two sub-reports? Can i use
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{newVehiclesList}) as a datasource for sub report 1 and
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{usedVehiclesList}) as datasource for sub report 2?
Is there anything else that needs to be done apart from what i mentioned? Do i need to create and pass any variables? Is the appropriate use of the list of lists as i have listed above or is it $F{detailRows}.get(0)?
I created a field detailRows in the main report as type list. I then pass the following to the subreport data source expression, new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{detailRows}
Is there any way i can pass the newVehiclesList from detailRows to the sub-report?
Thanks!
Selecting your SubReport you can set the property "Connection type" as "Use a data source expression" and inside the property "Data Source Expression" you set this:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{yourFieldHere})
Where your "yourFieldHere" is a list (don't forget to set the "Field Class" inside your field properties as a java.util.List as well)
Ok, then you need create two fields with the Field Class as java.util.List, one for each list (newVehiclesList and usedVehiclesList).
Put your two SubReports wherever you want and click on each one doing the following steps:
Change the "Connection type" to "Use a datasource expression" then change the "Data Source Expression" to new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{yourField})
Done.
ps: In order to use the fields inside your newVehiclesList and usedVehiclesList you have to create them inside of their own subReports.
i was the same problems with you and i solved it using the tag List of jasper, i used datasource in my class java, for example:
parameter.put("MyList", new JRBeanCollectionDataSource(ListObjects));
in JRXML
In palete of Jasper, choose the tag LIST and drag and drop in your relatory
after choose
create new dataset
create new dataset from a connection… ...
in data adapter choose new data adapter - collection of javabeans
use a JRDatasource expression
go in lis of parameters and choose you list op objects (MyList)
now go to outline of jasper and
- dataset properties
- edit and query filter ... ...
- javabean
- search you class (I using eclipse, so it's easy to search my class)
- add fields to use

Is there a way to create a so-called multi-typed model field?

Preface
I need to have objects (Object model) with an individual set of fields (Field model). It contains name and type (see the diagram). Each connection between Object and Field stores the field's value. Datatype of value depends on the Field type property and physically the value will be stored in one of the predefined db columns (value_number, value_text, ...).
How I want it to work:
field = Field.objects.get(pk=1)
sought_for = Object.fields.filter(field=field, value='test')
Is there a way to create such a field that can be put to QuerySet just as simple as in the example but it actually, depending on the field's type, uses different db column or even columns as I suppose that in the future there will types that involve more than one column to store its value.
P.S. I tried some EAV applications but they seemed to be too complicated for my case.
The diagram:
Field model, it stores name and type of fields
FieldValue, the model the values for the fields are stored.
UPD: Eventually I came to a thought that the very approach to use Postgres (or any relational database) is not the best choice. I got this implemented easily in MongoDB.

Django - How to annotate QuerySet using multiple field values?

I have a model called "Story" that has two integer fields called "views" and "votes". When I retrieve all the Story objects I would like to annotate the returned QuerySet with a "ranking" field that is simply "views"/"votes". Then I would like to sort the QuerySet by "ranking". Something along the lines of...
Story.objects.annotate( ranking=CalcRanking('views','votes') ).sort_by(ranking)
How can I do this in Django? Or should it be done after the QuerySet is retrieved in Python (like creating a list that contains the ranking for each object in the QuerySet)?
Thanks!
PS: In my actual program, the ranking calculation isn't as simple as above and depends on other filters to the initial QuerySet, so I can't store it as another field in the Story model.
In Django, the things you can pass to annotate (and aggregate) must be subclasses of django.db.models.aggregates.Aggregate. You can't just pass arbitrary Python objects to it, since the aggregation/annotation actually happens inside the database (that's the whole point of aggregate and annotate). Note that writing custom aggregations is not supported in Django (there is no documentation for it). All information available on it is this minimal source code: https://code.djangoproject.com/browser/django/trunk/django/db/models/aggregates.py
This means you either have to store the calculations in the database somehow, figure out how the aggregation API works or use raw sql (raw method on the Manager) to do what you do.