I have a web application (asp.net + SQL db) and I'm mapping it with nHibernate with relative success =).
This web reads an Oracle db (from an ERP system), collects and display data from it but its not using nHibernate for this task.
I have done several reports each one with its own vb.net class that I fill with intrincated queries,
collecting data from a bunch of Oracle tables. So my question is:
Can i do an specific hql query and fill my vb.class in a custom way (Maybe not mapping it), specifying one by one which column of my query fills each property?
===================== EDIT WITH SOLUTION =====================
Just if someone need the resolution, I post the solution in an example.
Public Class classExample
Private pProperty1 As Decimal
Private pProperty2 As String
Public Property Property1() As Decimal
Get
Property1 = pProperty1
End Get
Set(ByVal Value As Decimal)
pProperty1 = Value
End Set
End Property
Public Property Property2() As String
Get
Property2 = pProperty2
End Get
Set(ByVal Value As String)
pProperty2 = Value
End Set
End Property
Public Overloads Function Load() As System.Collections.Generic.List(Of classExample)
Using session As NHibernate.ISession = ISessionFactory.OpenSession()
Dim strsql As String = "SELECT.... FROM...."
Dim Query As NHibernate.IQuery = session.CreateSQLQuery(strsql)
Query.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(Of classExample))
Return Query.List(Of classExample)()
End Using
End Function
End Class
Thanks for your help.
You need to use a result transformer as here (see second code block).
Related
Hi there have a list of custom objects that I need to be able to sort, one property within another. How do I go about doing this using .Net. Normally I would carry out all sorting requirements within the SQL that delivers the data, unfortunately in this case I don't have control over that generation of the raw data. I consequently have no experience of sorting content using .Net functions such as Linq and/or IComparable. An impression of the code elements involved are listed below:
Public Class CustomObject
Public Property PrimaryItem As String
Get
Return _primaryItem
End Get
Set(ByVal value As String)
_primaryItem = value
End Set
End Property
Public Property SecondaryItem As String
Get
Return _secondaryItem
End Get
Set(ByVal value As String)
_secondaryItem = value
End Set
End Property
End Class
Public Class CustomObjectList
Inherits List(Of CustomObject)
Public Sub New()
End Sub
End Class
In essence I want to be able to sort CustomObjectList according to SecondaryItem within PrimaryItem:
PrimaryItem1
SecondaryItem1
SecondaryItem2
etc...
PrimaryItem2
SecondaryItem1
SecondaryItem2
etc...
etc...
Hoping that some kind person will be able to give me a 'leg up' in usage of either Linq and/or iComparable.
Dim result = MyList.OrderBy(Function(x) x.PrimaryItem).ThenBy(Function(x) x.SecondaryItem)
I'm combining two lists in visual basic. These lists are of a custom object. The only record I want to combine, are the once with a property doesn't match with any other object in the list so far. I've got it running. However, the first list is just 1.247 records. The second list however, is just short of 27.000.000 records. The last time I successfully merged the two list with this restriction, it took over 5 hours.
Usually I code in C#. I've had a similar problem there once, and solved it with the any function. It worked perfectly and really fast. So as you can see in the code, I tried that here too. However it takes way too long.
Private Function combineLists(list As List(Of Record), childrenlist As List(Of Record)) As List(Of Record) 'list is about 1.250 entries, childrenlist about 27.000.000
For Each r As Record In childrenlist
Dim dublicate As Boolean = list.Any(Function(record) record.materiaalnummerInfo = r.materiaalnummerInfo)
If Not dublicate Then
list.Add(r)
End If
Next
Return list
End Function
The object Record looks like this ( I wasn't sure how to make a custom object in VB, and this looks bad, but it worked):
Public Class Record
Dim materiaalnummer As String
Dim type As String 'Config or prefered
Dim materiaalstatus As String
Dim children As New List(Of String)
Public Property materiaalnummerInfo()
Get
Return materiaalnummer
End Get
Set(value)
materiaalnummer = value
End Set
End Property
Public Property typeInfo()
Get
Return type
End Get
Set(value)
type = value
End Set
End Property
Public Property materiaalstatusInfo()
Get
Return materiaalstatus
End Get
Set(value)
materiaalstatus = value
End Set
End Property
Public Property childrenInfo()
Get
Return children
End Get
Set(value)
children = value
End Set
End Property
End Class
I was hoping that someone could point me in the right direction to shorten the time needed. Thank you in advance.
I'm not 100% sure what you want the output to be such as all differences or just ones from the larger list etc but I would definitely try do it with LINQ! Basically sql for vb.net data so would something similar to this:
Dim differenceQuery = list.Except(childrenlist)
Console.WriteLine("The following lines are in list but not childrenlist")
' Execute the query.
For Each name As String In differenceQuery
Console.WriteLine(name)
Next
Also side-note i would suggest not calling one of the lists "list" as it is bad practice and is a in use name on the vb.net system
EDIT
Please try this then let me know what results come back.
Private Function combineLists(list As List(Of Record), childrenlist As List(Of Record)) As List(Of Record) 'list is about 1.250 entries, childrenlist about 27.000.000
list.AddRange(childrenlist) 'combines both lists
Dim result = From v In list Select v.materiaalnummerInfo Distinct.ToList
'result hopefully may be a list with all distinct values.
End Function
Or Don't combine them if you dont want to.
I want to save a communication inside a class. After that I plan to serialize the class o a XML file, where all datapoints are decoded between a tag.
Therefore I want to explain my communication protocol first.
The message Frame Looks like the following
LIE01
LIE02
When the communication ends, I have around 3000 of this telegrams inside a raw variable.
Here I describe the Messages:
LIE01: Header + 1 data word
LIE02: Header + 2 data words
My idea was to decode the frame and save it in a list (or array) of structures that are public properties of my class.
Public Class Com
Public Structure sLIE01
Public Property Header As Int16
Public Property data1 As Int16
End Structure
Public Structure sLIE02
Public Property Header As Int16
Public Property data1 As Int16
Public Property data2 As Int16
End Structure
Public Property LIE01 As List(Of sLIE01)
Get
?
End Get
Set(ByVal value As List(Of sLIE01))
?
End Set
End Property
Public Property LIE02 As List(Of sLIE02)
Get
?
End Get
Set(ByVal value As List(Of sLIE02))
?
End Set
End Property
End Class
Unfortunatelly I am more a beginner than an expert, so that I have no idea, how to write the code to Set or Get a specific LIE message.
Even I'm not sure, whether my way is a common way for this purpose or not.
You could use auto implemented properties in your code and skip getters and setters altogether (https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/auto-implemented-properties). You'll then be able to assign lists to them like:
Dim newList as new List(of sLIE01)()
ComInstance.Lie01 = newList
You can also operate on those list properties directly (just make sure you initialize them in class constructor to avoid NullReferenceException):
Dim lie as sLie01
ComInstance.Lie01.Add(lie)
Also consider replacing structures with classes: https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/structures-and-classes
If you still want to use Get Set it would look like this...
Private _LIE01 As List(Of sLIE01)
Public Property LIE01 As List(Of sLIE01)
Get
Return _LIE01
End Get
Set(value as List(Of sLIE01))
_LIE01 = value
End Set
End Property
VS2013, vb.net
For this class (only the relevant properties are displayed):
Public Class UserPost
Public Property Title As String
Public Property Topic As String
Public Property Type As ChannelType 'ChannelType is an Enum
End Class
The following query returns a simple list(of string) holding the titles of the UserPosts with Topic = topic:
Dim rtnList As New List(Of String)
rtnList = db.UserPost.Where(Function(x) x.Topic = topic).Select(Function(x) x.Anchor.Title).ToList()
But it would be useful to also report the ChannelType as a prefix to the Title. I could create a more complicated object to receive 2 columns and combine them later, but I wondered if there is a way to concatenate the columns in the query so that the rtnList receives the result of:
ChannelType.tostring() & Title
without having to code that afterword.
Of course there is. You just do pretty much exactly what you said. Instead of returning x.Anchor.Title you return x.Anchor.ChannelType.ToString() & x.Anchor.Title.
Very simple update. It simply fails, no error, no change gets made to the database.
Dim db As New BarClassesDataContext
Dim foo = (From a In db.articles Where a.id = 14 Select a).Single
Response.Write("<h3>" & foo.title & "</h3>")
foo.title = "This is my new, updated title for article ID #14"
db.SubmitChanges()
Here is the relevent portion of my article class. Also, this is a web form so I have no console. Is there another way to view the T-SQL output?
<Table(Name:="dbo.article")> _
Partial Public Class article
Private _id As Integer
Private _issueid As Integer
Private _dateadded As Date
Private _title As String
Private _titlelink As String
Private _description As String
Private _image As String
Private _imagelink As String
Private _type As Integer
Public Sub New()
MyBase.New
End Sub
<Column(Storage:="_id", AutoSync:=AutoSync.Always, DbType:="Int NOT NULL IDENTITY", IsDbGenerated:=true)> _
Public Property id() As Integer
Get
Return Me._id
End Get
Set
If ((Me._id = value) _
= false) Then
Me._id = value
End If
End Set
End Property
If you have any invalid fields definitions you may have an issue where 0=1 is added to the WHERE clause. Check that all of your non-nullable fields are set. (I fought with this for about two hours one night while watching the SQL profiler add the extra 0=1 for no reason.)
Post on Social MSDN
Another post about this issue
(If I can find the issue on connect.microsoft.com I will post it as well)
At first glance, I would say one (but not the main) problem is '='. I think it needs to be:
Dim foo = (From a In db.articles Where a.id == 14 Select a).Single
The main problem I see is the lack of an UpdatetOnSubmit() statement. How does L2S know you want to do an Update?
Try
db.Articles.UpdateOnSubmit(foo);
db.SubmitChanges();
or something close to this.
My suspicion is that you don't have a properly defined primary key in your database and/or in your System.Data.Linq.Mapping attributes. Show us the Article class - that will likely be where your problem is. Make sure you have an IDENTITY field in your database, and be sure your linqed up class has got IsPrimaryKey:=True and IsDbGenerated:=True in the <Column> attribute.
Also, it would be wise to set the .Log property on your DataContext to see what SQL is being executed. I like the debug window logger technique, which I have mentioned before here: DataContext SubmitChanges in LINQ