|
I found this one example is this the best method / approach?
<pre>namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
List<Customer> CustomerList = new List<Customer>();
CustomerList.Add(new Customer { ID = 1, Name = "One", GroupID = 1 });
CustomerList.Add(new Customer { ID = 2, Name = "Two", GroupID = 1 });
CustomerList.Add(new Customer { ID = 3, Name = "Three", GroupID = 2 });
CustomerList.Add(new Customer { ID = 4, Name = "Four", GroupID = 1 });
CustomerList.Add(new Customer { ID = 5, Name = "Five", GroupID = 3 });
CustomerList.Add(new Customer { ID = 6, Name = "Six", GroupID = 3 });
IEnumerable<CustomerGrouping> CustomerGroupings = CustomerList.GroupBy(u => u.GroupID)
.Select(group => new CustomerGrouping { GroupID = group.Key, Customers = group.ToList() })
.ToList();
}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int GroupID { get; set; }
}
public class CustomerGrouping
{
public int GroupID { get; set; }
public IEnumerable<Customer> Customers { get; set; }
}
|
|
|
|
|
mjeeves wrote: s this the best method / approach It all depends on what you are trying to achieve.
|
|
|
|
|
 here is another example data set, i want to go one more level down and group by username and EventTime, but i don't know how to expand the linq, plus i will have to remove the time stamp from the event dattime
<pre>namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
List<SignInReport> SignInReports = new List<SignInReport>();
SignInReports.Add(new SignInReport { UserName = "Bob", EventName = "SignIn", EventTime = new DateTime(2020,1,18,6,0,0) });
SignInReports.Add(new SignInReport { UserName = "Bob", EventName = "SignOut", EventTime = new DateTime(2020, 1, 18, 10, 0, 0) });
SignInReports.Add(new SignInReport { UserName = "Bob", EventName = "SignIn", EventTime = new DateTime(2020, 1, 18, 11, 30, 0) });
SignInReports.Add(new SignInReport { UserName = "Bob", EventName = "SignOut", EventTime = new DateTime(2020, 1, 18, 16, 30, 0) });
SignInReports.Add(new SignInReport { UserName = "Charlie", EventName = "SignIn", EventTime = new DateTime(2020, 1, 18, 6, 0, 0) });
SignInReports.Add(new SignInReport { UserName = "Charlie", EventName = "SignOut", EventTime = new DateTime(2020, 1, 18, 15, 30, 0) });
SignInReports.Add(new SignInReport { UserName = "Charlie", EventName = "SignIn", EventTime = new DateTime(2020, 1, 19, 6, 30, 0) });
SignInReports.Add(new SignInReport { UserName = "Charlie", EventName = "SignOut", EventTime = new DateTime(2020, 1, 19, 17, 45, 0) });
IEnumerable<SignInReportGrouping> SignInData = SignInReports.GroupBy(u => u.UserName)
.Select(group => new SignInReportGrouping { UserName = group.Key,
SignInReports = group.ToList() })
.ToList();
}
}
public class SignInReport
{
public string UserName { get; set; }
public string EventName { get; set; }
public DateTime EventTime { get; set; }
}
public class SignInReportGrouping
{
public string UserName { get; set; }
public IEnumerable<SignInReportDay> SignInReportDay { get; set; }
}
public class SignInReportDay
{
public IEnumerable<SignInReport> SignInReports { get; set; }
}
}
|
|
|
|
|
Since you're using Linq-to-Objects, that's simple enough to do:
IEnumerable<SignInReportGrouping> SignInData = SignInReports
.GroupBy(u => u.UserName)
.Select(user => new SignInReportGrouping
{
UserName = user.Key,
SignInReportDay = user
.GroupBy(u => u.EventTime.Date)
.Select(day => new SignInReportDay
{
Day = day.Key,
SignInReports = day.ToList(),
})
.ToList(),
})
.ToList(); You'll probably want to add the Day property to your SignInReportDay class so you know which day it represents:
public class SignInReportDay
{
public DateTime Day { get; set; }
public IEnumerable<SignInReport> SignInReports { get; set; }
}
NB: If you're using Entity Framework 6, you probably won't be able to use the .Date property. For EF6, use DbFunctions.TruncateTime :
.GroupBy(u => DbFunctions.TruncateTime(u.EventTime)) For EF Core, the .Date property should work correctly.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have a custom font. The font contains symbols at specific addresses. I want to display one of the symbols in a TextBlock.
The font (ttf-file) is located in my applications's folder: Fonts.
I have included the font in the application and set the BuildAction to Resource .
I have verified the font's name (not file name) to reference it in my code.
I have verified which "address" the symbol should be fetched from in the font.
I have tried this both in XAML and with C#. But it won't work.
XAML
<Window x:Class="Main.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Main"
mc:Ignorable="d"
Loaded="Window_Loaded"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="Foo">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#MyFont" />
</Style>
</Window.Resources>
<StackPanel Orientation="Vertical">
<TextBlock Text="&#x E905;" Style="{StaticResource Foo}" />
<TextBlock FontFamily="Segoe MDL2 Assets" Text="&#x E777;" />
<TextBlock x:Name="txt1" />
<TextBlock x:Name="txt2" />
</StackPanel>
</Window> Notice: Never mind the space in the middle of "address" for the first and second TextBlock. The code-view here at CodeProject failes to display the address. It tries, and fails, to fetch the correct symbols by that address - if I remove the space in the addresses...
C#
Here is the handler for the Loaded event :
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txt1.FontFamily = new FontFamily("pack://application:,,,/Main;component/Fonts/#MyFont");
txt1.Text = "\xe905";
txt2.FontFamily = new FontFamily("Segoe MDL2 Assets");
txt2.Text = "\xe7777";
} Result
The result are displayed in the four TextBlocks. From top to bottom:
* First: This fails. I get an empty rectangle, like these rectangles.
* Second: This line works. You should be able to copy this line into your own application with success.
* Third: Same as the first TextBlock. An empty rectangle.
* Forth: This "works". I get the correct symbol as well as an empty rectangle.
What could be the problem?
|
|
|
|
|
As far as I can see, you can't use a full pack: URI to reference an embedded font:
In order to reference font resource items from code, you must supply a two-part font resource reference: the base uniform resource identifier (URI); and the font location reference.
If it's a single application, try using ./Fonts/#MyFont from XAML, and new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#MyFont") from code.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks for the reply.
Unfortunately it didn't work. Changing the syntax for the reference of the Font didn't solve the problem. Thanks for suggesting that anyway. It was worth a try.
For this question I have created a simple application where the Fonts are stored in the same project as my executable. I can reproduce the error in this small application for myself and to post in this forum.
In my real project I have all this is in a WPF User Control Library. That is way I have the exact path to the font with the assembly reference.
|
|
|
|
|
Show the code you tried; maybe you didn't get it right.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I have tried this:
<Window.Resources>
<Style x:Key="Foo">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#MyFont" />
</Style>
<Style x:Key="Bar">
<Setter Property="TextElement.FontFamily" Value="./Fonts/#MyFont" />
</Style>
<FontFamily x:Key="Baz">./Fonts/#MyFont</FontFamily>
</Window.Resources>
<StackPanel Orientation="Vertical">
<TextBlock Text="" Style="{StaticResource Foo}" />
<TextBlock Text="" Style="{StaticResource Bar}" />
<TextBlock Text="" FontFamily="{StaticResource Baz}" />
<TextBlock FontFamily="Segoe MDL2 Assets" Text="" />
<TextBlock x:Name="txt1" />
<TextBlock x:Name="txt2" />
</StackPanel>
In the code, I have tried these different ways to set the FontFamily property. I have just listed them here. But have run them separately. All failed...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txt1.FontFamily = new FontFamily("pack://application:,,,/Main;component/Fonts/#MyFont");
txt1.FontFamily = new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#MyFont");
txt1.FontFamily = new FontFamily("MyFont");
txt1.Text = "\xe905";
} Notice the txt1.FontFamily = new FontFamily("MyFont");
I have installed the font from it's ttf-file.
If I open the Character map application in Windows I can see all symbols that I require.
I just can't understand what is going on...
|
|
|
|
|
I have solved it!
It was the darn TTF-file which was the problem.
When I changed to another font, the XAML-code worked straight away!
|
|
|
|
|
Previously when debugging my program would stop if an error occured, this no longer works.
Now I have to hunt down the error which takes hours sometimes.
Is there an option / setting I can change in order to stop on the error as it occurs.
I appreciate any help.
Thanks in advance.
Michael
|
|
|
|
|
|
Debuggers don't "stop on errors" they stop either because they hit a breakpoint you set, or an exception occurs that you aren't handling (or that is in the "break on this exception" list your project has set).
It's worth checking two things:
1) That you are actually running your app in the debugger. Yes, yes - I know that's obvious. But you'd be surprised how many people ignore the obvious ...
2) That you haven't switched exceptions off: Go to "Debug...Windows...Exception Settings" and check that "Common Language Runtime Exceptions" is a solid black.
But logic errors in your code the debugger won't automatically stop on - it has no idea what you are trying to do, so it can't! (But you knew that anyway ...)
"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!
modified 7-Sep-20 12:04pm.
|
|
|
|
|
OriginalGriff wrote: an exception occurs that you aren't handling (and that is in the "break on this exception" list your project has set)
The "break on this exception" setting overrides the unhandled part. You use it to break on exceptions which are handled in code. The debugger will always break on unhandled exceptions.
Manage exceptions with the debugger - Visual Studio | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Monday morning mode ...
"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 hundred of DataTables and many of the caption names within do not match the column name.
Is there a way I can change all captions to match column name.he follows code I tried.
foreach (DataTable dt in dataSet.Tables)
{
foreach (DataColumn dc in dt.Columns)
{
if (dc.DataType == typeof(string))
{
dc.Caption = dc.ColumnName.ToString();
}
}
}
'
However this is not permanent and I don't see results.
Is there a way to make this permanent.
I appreciate any help and thanks in advance.
Michael
|
|
|
|
|
The Caption property isn't used to set the headers of a DataGridView (for example) when you use it as a DataSource - it probably should have been, but it definitely wasn't!
If you want a "friendly name" for your displayed columns, you need to manually set them on the display control via the "HeaderText" property:
myDataTable = new DataTable();
myDataTable.Columns.Add("C1");
myDataTable.Columns.Add("C2");
myDataTable.Columns[0].Caption = "A new caption";
myDataGridView.Columns.Clear();
myDataGridView.DataSource = myDataTable;
for (int i = 0; i < myDataTable.Columns.Count; i++)
{
myDataGridView.Columns[i].HeaderText = myDataTable.Columns[i].Caption;
}
"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 a simple html-document with a button and when someone clicks the button, I would like to execute a C# method. Is this possible? There is no Internet or server involved in this, the html-file and the output (.dll? .exe? something else?) of my C# code will all reside in the same folder on my computer.
|
|
|
|
|
Try:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> Bear in mind that this will do a postback to the Server with all that entails.
To be a bit more sophisticated about it, you would have to use AJAX: AJAX In ASP.NET[^]
"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!
|
|
|
|
|
But there is no server, just a client.
|
|
|
|
|
You can't run C# code on a client - it needs the right .NET framework and that may not be installed.
And besides, if you didn't have a server you would have nothing to load the HTML from in the first place!
I think you need to have a good long think about exactly what you are trying to do, and perhaps research some to find out how these things work. I get the feeling you are just guessing, and that's not a good idea.
"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!
|
|
|
|
|
You probably need to run your HTML in a web browser control, in a Windows Forms or WPF app, and then hook up to events in the HTML. "Tagging" hidden content with "scripting code" is an added option.
internet explorer - Hooking IE Events in C# - Stack Overflow
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Can a button in html call any function at all? If so, in what programming language? Could I in that case make that programming language call a .dll consisting of my compiled C# code?
|
|
|
|
|
A button is "dumb"; it doesn't "call anything and everything"; it needs to be "hooked up"; regardless of the platform. You're interfacing with a "document object model". An API.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
An HTML button cannot directly call a C# method. The button is in one execution environment and the C# code is in another, even if your HTML and C# code are running client-side.
It sounds like you're trying to use an HTML page as an interface for a desktop app. That's a bad idea.
It can work, but there's a steep price. You'd have to host the ASP.NET runtime in your desktop app. This adds a LOT of overhead and complexity to your app, so it's usually not worth it.
If you're going to go through this pain, it's actually easier to just write up the app as an actual website and host in on the internet.
|
|
|
|
|