|
Hi All,
I'm looking to create a set of stats from a DataTable and I'm currently having a bit of trouble working out the best way to do it. There are a few columns in the table that I'm interested in with an unknown number of possible values in each of these columns. I have defined a Statistic type like:
public class Statistic
{
public string StatNameTag { get; set; }
public string Description { get; set; }
public int Value { get; set; }
public double Percentage { get; set; }
public Statistic()
{
}
}
Which are in turn held in:
public static ObservableCollection<statistic> StatsCollection = new ObservableCollection<statistic>();</statistic></statistic>
Currently I'm thinking that a foreach loop over the table is the way forward. Each time a new value in a given table column is found I'll create a new Statistic object to hold the information in and give it a StatTagName that includes the value in the name which will provide a way to find out if its a new value or not. If its not a new value increment the value on the current Statistic object already in the collection.
Am I going in the right direction with this, or would it make more sense to use the .Compute() method or something else altogether? Or should I skip this altogether and execute a series of ExecuteScalar() commands directly on the database after performing a distinct select on each column i'm interested in?
I'm struggling with the best way to go with this. Everything else seems a bit expensive compared to a foreach loop over an already loaded DataTable object.
Any input would be great. TIA,
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hmmm - rather than using a DataTable, why not take a look at using Linq to do this? It would look something like:
var p1 = from p in dataContext
group p by p.Name
into MyGroup
select new { MyGroup.Description, MyGroup.Group.Sum(x=>x.Percentage), MyGroup.Group.Sum(y=>y.Value), MyGroup.Description }; I'm typing this blind, so it might not be 100% accurate, but you should get the idea.
|
|
|
|
|
That does look interesting indeed!! I'll have a play around with this method. Thanks Pete.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
I've gotten so far with this:
public void UpdateStats()
{
var p1 = from p in context.LocalSamples
group p by p.FileBitRate into MyGroup
select new {description = MyGroup.Key, percent = MyGroup.Group.Sum(x=>x.Percentage), value = MyGroup.Group.Sum(y=>y.Value) };
}
However, MyGroup doesn't appear to have a .Group method, only a .GroupBy<> but I've seen stacks of examples online using .Group.Sum ... so I'm a bit confused by that. I was also seeing errors saying:
"Invalid anonymous type member declarator"
Adding in description =, percent = solved that one, but I'm still unsure why I'm seeing a problem with the .Group.Sum ...
Tia,
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Sorry mate. Change this to:
var p1 = from p in context.LocalSamples group p by p.FileBitRate into MyGroup select new {description = MyGroup.Key, percent = MyGroup.Sum(x=>x.Percentage), value = MyGroup.Sum(y=>y.Value) };
|
|
|
|
|
Ahhh, thanks fella ... will have a look at this later tonight.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hi Again Pete,
On these two elements of the linq code:
percent = MyGroup.Sum(x=>x.Percentage), value = MyGroup.Sum(y=>y.Value
I get the following error:
DataLayer.LocalSample' does not contain a definition for 'Percentage'
DataLayer.LocalSample' does not contain a definition for 'Value'
Should the MyGroup (is this an anonymous type?) actually be an instance of my Statistic class? Or should I be creating the Statistic objects in a foreach loop over the p1 variable once this code has been executed?
the x. / y. is expecting a field from my LocalSample table and Percentage/Value are the things I'm trying to calculate. Bits of this are clearer to me now, but I'm still way off really understanding this.
Thanks chap.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hi,
Can anyone tell me the C# equivalent ofthe following java code?
address = InetAddress.getLocalHost().getAddress();
I've not done anything with networking in C# before but from an admittedly quick inspection it does appear that the java implementation makes more intuitive sense. I'd say it's more feature rich too but I haven't ben able to make complete sense yet of what C# has to offer.
Regards,
Dave
Regards,
Dave
|
|
|
|
|
DwR wrote: Can anyone tell me the C# equivalent ofthe following java code?
address = InetAddress.getLocalHost().getAddress();
IPAddress address = Dns.Resolve("localhost");
|
|
|
|
|
Hi,
Thanks for that. A couple of questions / observations:
1. The C# documentation refers to the resolve method as being obsolete.
2. The java type of "address" is byte[]. The method returns a "raw" ip address. The C# equivalents, including Dns.resolve return an IPAddress[] type which i assume isn't raw. Can C# return raw addresses? I assume the type to use is c# byte[]...
Additionally, I'm unclear as to what the java documentation means by a raw ip address. Can you clarify? I'm expecting that it means that I the IPAddress[] type is not the correct type for this operation.
Regards,
Dave
|
|
|
|
|
DwR wrote: I'm unclear as to what the java documentation means by a raw ip address
One would presume that the raw IP Address is just the bytes that make up the IP Address - as it would normally be with anything described as raw.
DwR wrote: I'm expecting that it means that I the IPAddress[] type is not the correct type for this operation
What operation?
|
|
|
|
|
Hi,
I think I've resolved this. By setting the type as IPAddress the member "GetAddressBytes" can be used to convert the address to bytes matching the type of the variable "address". Thanks for your tip, it pointed me in the right directions.
That said, I've hit another problem which seems to be a bit trickier. I need to determine the IP addresses associated with the network interfaces of the multi-homed host the code is running on. I am capturing the available interfaces as follows:
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
I then want to loop through the entries and for each one load it's associated IP Addresses into an IPHostEntry instance (or similar). However, I don't see any way to determine the list of ip addresses associated with a particular network interface returned by the foreach loop as it iterates through "interfaces". How is this done?
Regards,
Dave
|
|
|
|
|
Hello, i`m a newbie when it come`s to programming, i have read a lot, and made 1 proggy about 1.5 years ago and now i have forgotten most of the language..
So here`s the case: I got 2 richTextBoxes on 1 page, so when i load 2 diffrent files into each of the 2 rtb`s, i wan`t to make a function that does: if richTextBox1 is containing any of the words/numbers/signs that is loaded to richTextBox2, i wan`t the proggy to delte those from richTextBox2 and only keep the uniqe words/numbers/signs, so the user can save it..
Could any please help a newbie out with this one
Help is highly apereachiated
Thank you
|
|
|
|
|
The first thing that occurs to me is that you'll probably want to use a regular expression on the first textbox to retrieve the list of words in it. Possibly, you'll want to add the entries to a unique list so that you won't be performing unnecessary lookups on the second textbox. Then, you'll perform a regex search in the second textbox based on these results (respecting word boundaries of course) and remove the offending items using the replace method. And before you ask, I'm not going to give you the code to do this - it's quite lengthy.
|
|
|
|
|
Thank you for the reply Pete O'Hanlon
Yeah i figured out this code would be lenghty, i wasn`t either gonna ask u to do that code for me, cos i like doing the work my self. What i was hoping for where some example`s, maybe some links to some reading sources about this and or a comment like "u can do it if u take ur time" or "forget it, ur to unexperienced, maybe in a year or 2"
Nice tip: "Possibly, you'll want to add the entries to a unique list so that you won't be performing unnecessary lookups on the second textbox"
Thank you
|
|
|
|
|
I'm not going to talk down to you. This isn't a particularly arduous piece of code. The thing you need to look up is regexes with word boundaries.
|
|
|
|
|
Ok m8, thank you for the reply again, i`m gonna do some research on that
Cheers
|
|
|
|
|
Hi,
Does anyone have an example to get me started on recording to an mp3 file.
I want to record from the microphone and save it directly to an mp3.
I have a working project but now I save it to wav and then send it to lame and I want to skip that step.
Thanks,
/Johan
|
|
|
|
|
Hello,
the only program i know that streams "directly" to a desired audio format is the VLC Media Player. The good news is, that it is open source and quite well documented. But... it's unmanaged (of course) and i think you will have to dig through quite a bit of code in order to find something useful.
Anyway, try this:
http://www.videolan.org/developers/vlc.html[^]
Michael.
|
|
|
|
|
Thanks for your answer but I was hoping that there might be a simple library I could use
|
|
|
|
|
Check this[^] article - it uses the dll from Lame. Creating your own encoder will be a LOT of hard work and probably not possible in C#?
Dave
|
|
|
|
|
That article does pretty much what I have so far, it takes a wav-file and uses lame to create an mp3.
I want to record directly to mp3.
|
|
|
|
|
AFAIK, I could be wrong (but I've been around the pro audio world for over 20 years) it's not possible.
MP3 is an encoding format that compresses existing audio.
It's like trying to write data to a zip file without first either saving the file or creating it in memory - the file has to exist somewhere first.
Dave
|
|
|
|
|
Yes, I realize it has to created in memory or something like that, so what I'm looking for is a free audio library that takes the data from the mic and sends it directly to lame. My problem is that it must be free for commercial use.
|
|
|
|
|
Just had a quick look at the source code from the article I referenced.
It uses its own class WaveStream which takes a Stream in the constructor. I'm pretty sure that would work with a MemoryStream , and if not, I can't imagine it'd be too difficult to ads a new overload to the constructor. Then you could create the wav in memory (MemoryStream) and write directly to disk as mp3.
Dave
|
|
|
|