|
What does GroupBy return?
What type is x?
Why would you assume that ToString would do anything useful?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have never used LinQ, and as such I am a bit unsure what GroupBy returns and what type x is.
And about the ToString, I was just testing, all (or many) does...
Here is the whole code I have, if anyone is interested:
using System;
using System.Collections.Generic;
using System.Windows;
using System.IO;
using System.Linq;
namespace Inventory
{
public partial class Window1 : Window
{
void button1_Click(object sender, RoutedEventArgs e)
{
string[] lines = File.ReadAllLines(@"C:\Users\sdadmin\Desktop\Test data.txt");
var barcodes = lines.Select(line => {string[] parts = line.Split(',');
return parts[2]; });
var x = barcodes.GroupBy(b => b);
}
public class List
{
public string Date{get; set;}
public string Time{get; set;}
public string EAN {get; set;}
}
}
}
I am pretty new to C#, I have only been at it for about 1 month or so, if I disregard time when I don't do any programming...
Have a nice day!
|
|
|
|
|
If you don't know what type something is, you can hover the mouse over it, and VS will tell you.
In this case it's a IEnumerable<IGrouping<string, string>>
Which means that it's a collection of Groups of string key / string value pairs.
Each Group in the collection has two properties you are interested in: the Key (which is the barcode number) and the Count method which tells you how many items are in each group.
So it's pretty simple to access the barcode and count:
string[] lines = File.ReadAllLines(filePath);
var barcodes = lines.Select(line => {
string[] parts = line.Split(',');
return parts[2];
});
var x = barcodes.GroupBy(b => b);
foreach (var y in x)
{
Console.WriteLine($"{y.Key} : {y.Count()}");
}
For your sample data you get:
1578456329781 : 1
1578453697235 : 3
1548795358155 : 1
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks for the help!
That worked like a charm.
Was unfamiliar that you could hover the mouse over the variable and the IDE would tell you what type it was.
On the PC I am on now, at work, I can't install a newer framework, so I had to redo the code to not use String interpolation, sadly.
Also I am using SharpDevlop.
Anyhow, thanks for the help
Have a nice day!
|
|
|
|
|
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Quote: I was thinking on using one loop to go threw the list, and then use the second loop to check for duplicates and count. You are right, it is not just about finding/removing the duplicates, but also counting them.
For a custom solution, you can scan the list and create a Dictionary<string, int> that holds the times that a key occurs; check if the key exists, then increment the value, otherwise, add the key with 1 as value. The algorithm will run O(N) in a single go, and you can then traverse on the Dictionary while performing your actions—and this part depends on how you use the Dictionary and what purpose you make it serve. You will be using a single loop, and the front-end framework (ListView) would then be able to render the results itself; which will again be a loop.
You might as well want to use LINQ or other "shiny" C# features to perform this as well, C# LINQ find duplicates in List - Stack Overflow
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Finding duplicates by barcodes is easy; i'd suggest you get your data into an "object" where it will be easy to perform any type of query on it: I'd use a Dictionary:
private readonly string bcdata = @"2020-07-08,13:54:16,1578456329781
2020-07-08,14:07:13,1578453697235
2020-07-08,14:08:03,1548795358155
2020-07-08,14:11:09,1578453697235
2020-07-08,14:11:42,1578453697235";
private char[] splitwhtspc = {'\r', '\n', ' ', '\t'};
private char[] splitcomma = {','};
var lines = bcdata.Split(splitwhtspc, StringSplitOptions.RemoveEmptyEntries);
Dictionary<DateTime, Int64> dttobc = lines.Select
(
line => line.Split(splitcomma, StringSplitOptions.RemoveEmptyEntries)
).Select
(
sary =>
new
{
key = DateTime.Parse(sary[0] + " " + sary[1]),
value = long.Parse(sary[2])
}
)
.ToDictionary(
kl => kl.key,
vl => vl.value
); Using this Dictionary, you can easily create a new Dictionary of barcodes and number of barcodes:
Dictionary<long, int> dups = dttobc.Values.GroupBy(v => v).ToDictionary(g => g.Key, g => g.Count());
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Hello,
I has asked to create a POC on creating Entity in PowerApps CDS using C#. I don't know which API to be consumed in C#.
Could someone please guide me?
Thanks
Ramesh
|
|
|
|
|
|
con = new OleDbConnection("provider=oraoledb.oracle.1;user id=scott;password=tiger");
daEmp = new OleDbDataAdapter("select * from emp", con);
daDept = new OleDbDataAdapter("select * from dept", con);
//----------------------------
ds = new DataSet();
//---------------------------------------
daEmp.Fill(ds, "emp");
dataGridView1.DataSource = ds.Tables["emp"];
//-------------------------------------------
daDept.Fill(ds, "dept");
cmbDname.DataSource = ds.Tables["dept"];
cmbDname.DisplayMember = "dname";
cmbDname.ValueMember = "deptno";
//-------------------------------------------------
pictureBox1.DataBindings.Add("Image", ds.Tables["emp"], "photo2", true, DataSourceUpdateMode.OnPropertyChanged);
txtEmpno.DataBindings.Add("text", ds.Tables["emp"], "empno");
txtEname.DataBindings.Add("text", ds.Tables["emp"], "ename");
txtSal.DataBindings.Add("text", ds.Tables["emp"], "sal");
cmbDname.DataBindings.Add("selectedvalue", ds.Tables["emp"], "deptno");
dtpHireDate.DataBindings.Add("text", ds.Tables["emp"], "hiredate");
txtJob.DataBindings.Add("text", ds.Tables["emp"], "job");
OleDbCommandBuilder cmb = new OleDbCommandBuilder(daEmp);
bmb = this.BindingContext[ds.Tables["emp"]];
}
Im using the above code to insert picture of employee but when i Try to save it using
bmb.EndCurrentEdit();
daEmp.Update(ds.Tables["emp"]);
Im getting an error Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
I have tried to change the data type in Oracle from blob to long raw but Im getting the same issue.
Could u plz let me know what is the error
Table emp already having primary key
The problem occur only when the line binding of picturebox is ther.
If i committed the line of picturebox no error the record will be saved.
In Sql server I have tried same table and same column it is working.
In sql server I declare the column of employee photo as image data type.
But in oracle I have tried Long raw and blob data type it is not working
|
|
|
|
|
Ask in the Oracle forums; their database, they might know.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Am trying to read/write to card using RFID reader, but the RFID reader is appearing under Human Interface Device, so far I have been able to get all Human Interface Devices into a DropDownlist using HidLibrary here is my code
HidDevice[] mahdDevices;
private void Form1_Load(object sender, EventArgs e)
{
mahdDevices = HidDevices.Enumerate().ToArray();
for (int i = 0; i < mahdDevices.Length; i++)
{
cmbDevices.Items.Add(mahdDevices[i].Description);
}
}
But now am not sure of how to send and receive data from the HID selected on the drop downlist, I also checked Hardware IDs of RFID device in properties, No idea on what to do with the IDs or how to communicate with HID devices to scan card, this is how i usually communicate with devices using serial port:
_port = new SerialPort();
_port.PortName = comboBox2.Text;
_port.BaudRate = 9600;
_port.Parity = Parity.None;
_port.DataBits = 8;
_port.StopBits = StopBits.One;
if (_port.IsOpen)
{
textBox1.Clear();
}
else
{
_port.Close();
}
_port.DataReceived += new SerialDataReceivedEventHandler(RFID_DataReceiver);
Please I need help communicating with HIDs.
|
|
|
|
|
RFID Readers do not allow you to write to the chip. As the name implies, it READS, not writes. All it does is read ID tags that are programmed into the chips.
RFID Readers usually expose themselves as either a serial device (think COM ports of the old days), or as keyboards, where they "type" whatever code they read off the card. For this to work, you'd have to handle key input using your Forms KeyPreview event.
How the device exposes itself would be covered in the documentation for whatever reader you're using.
|
|
|
|
|
Am completely new to working with RFID, I just need some help reading the content of an RFID Card, I did something and i found a HidLibrary to get input from HID devices but I still finding it hard to use. Here is what i have done so far:
private void button4_Click(object sender, EventArgs e)
{
var devives = HidDevices.Enumerate(0x9915, 0x1116);
_device = devives.First();
_device.OpenDevice();
_device.Inserted += DeviceAttachedHandler;
_device.Removed += DeviceRemovedHandler;
_device.MonitorDeviceEvents = true;
_device.Write(new byte[] { 0, 81, 80 });
_device.ReadReport(OnReport);
_device.CloseDevice();
}
When it hits _device.write, I hear a couple of beep sounds I don't really know what that means..
private void DeviceAttachedHandler()
{
_device.ReadReport(OnReport);
}
private static void DeviceRemovedHandler()
{
Console.WriteLine("Device Removed");
}
private void OnReport(HidReport report)
{
if (!_device.IsConnected) { return; }
var cardData = report.Data;
str = Encoding.ASCII.GetString(cardData);
if (str == null)
{
_device.ReadReport(OnReport);
}
else
{
MessageBox.Show("String");
}
}
OnReport data returns 0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 I don't know what this means too any help please
|
|
|
|
|
I couldn't tell you. I know nothing about your device at all.
Frankly, going through a HID driver is doing it the hard way.
Like I said, it's going to be covered in the documentation of the device. If you don't have any documentation, nor vendor developer support, you bought the wrong device.
|
|
|
|
|
Okay the card reader is working fine in another software I want to implement that same functionality in my software, that's what this whole struggle about, and I don't have any documentation to look at.
|
|
|
|
|
If you've got no docs, you bought the wrong device. Anything you do is just guessing.
You try to figure out how the device exposes itself by doing some testing. If it's a "keyboard wedge", all you have to do is open Notepad, make sure it has the focus, the scan a card. If text shows up in Notepad, it's acting like a keyboard.
If that doesn't do anything, you can go into Device Manager and see if it exposes itself as a COM port device.
I worked with RFID hardware back about 15 years ago, but the devices I had were all Ethernet-based and I had all the documentation on them.
|
|
|
|
|
I have checked the device manager, it is seen in Human Interface Device, I got the hardware IDs hoping I could do something with the IDs but nothing yet
|
|
|
|
|
If it's showing us as a HID, you can see if it's working like a keyboard.
I already told you how to do that.
|
|
|
|
|
Dave is right: without information you are just whistling in the dark - you need to talk to the manufacturers of the reader, and see what manuals / data / sample code they can provide.
Just trying to work it out by assuming it's a HID and guessing is a very slow and clumsy way compared with reading the manual!
If it's a HID, then it's probably configured to act as a keyboard rather than to provide a "direct datastream" - but you can only confirm that by finding the maker.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello everyone! I need help with generating random decimal numbers from 5 to 6 (For example 5.1, 5.2 etc.) It often throws out various errors to me so I decided to contact the team on the forum. Any advice and suggestions will be helpful to me. I am a beginner in all this..
|
|
|
|
|
There is no direct support for random decimal numbers in .NET, but it's pretty easy to do:
private Random rand = new Random();
private decimal GetRandom(decimal min, decimal max)
{
return ((decimal)rand.Next((int)(min * 100.0M), (int)(max * 100.0M))) / 100.0M;
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Or, if you need more resolution:
private Random rand = new Random();
public decimal GetRandom(decimal min, decimal max, int steps=10000) {
if (steps<=0) throw new ApplicationException("steps must be >= 1");
int r = rand.Next(0, steps);
return ((steps-r)*min+r*max)/steps;
}
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
|
|
|
|
|
Yours a homework question, so my answer not going to be acceptable, but for your amusement, read on.
Sajo Nezic wrote: I need help with generating random decimal numbers from 5 to 6 (For example 5.1, 5.2 etc.) That's a question from a user. As a developer, you supposed to see that "5" is a constant and not relevant to the question. You generate a float and put a "5." string in front of it. Gives you instantly one more digit to play.
So the next thing the user wants is "random data". There's this thing called a "youtube" giving you very large blobs of random data. Not "random" enough for homework, but random enough for encryption using one-time-pads. The amount of data available is overwhelming.
Back to your position; you get multiple errors, and we cannot guess what wrong on "multiple", as the answer is "multiple". Show us your code, let us argue for a day and then come back. Your code cannot be more wrong than my first code, so just share and learn.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Sajo Nezic wrote: It often throws out various errors to me
You need to show the code and explain what are the errors, otherwise we can't help you to fix your code.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|