2 lines in the Debug.Log result C# - c#

How can I show the Debug.Log in 2 lines (like adding an enter or <br>) in C#?
I have lots of info to show and compare, yet it is not convenient to read it in one line.
Current result: Debug.Log("Pointer:" + " " + pointerPosX + " " + pointerPosY + " " + "target" + " " + targetPosX + " " + targetPosY);
Expected result:
Pointer: pointerPosX + " " + pointerPosY
Target: targetPosX " " " + targetPosY
P.S. This is my first question in StackOverflow - so, please, let me know if I did something wrong.

Option 1 (\n):
Debug.Log("Pointer:" + " " + pointerPosX + " "
+ pointerPosY + "\n" + "Target:" + " " + targetPosX + " " + targetPosY);
Option 2 (System.Environment.NewLine):
Debug.Log("Pointer:" + " " + pointerPosX + " " + pointerPosY +
System.Environment.NewLine + "Target:" + " " + targetPosX + " " + targetPosY);
Using $ - string interpolation
Option 1:
Debug.Log($"Pointer: {pointerPosX} {pointerPosY}\nTarget: {targetPosX} {targetPosY}");
Option 2:
Debug.Log($"Pointer: {pointerPosX} {pointerPosY}{System.Environment.NewLine}Target: {targetPosX} {targetPosY}");
Difference between "\n" and Environment.NewLine

Related

display the text on 2 different lines

listBox2.Items.Add(c[n].nume_proprietar + ", " + c[n].nume_catel + ", " + c[n].varsta + " ani , " + c[n].data_vaccin + " " + c[n].tip_vaccin);
Hi, I was wandering if i could insert a line in between " ani , " and c[n].data_vaccin so that my code will display on 2 different lines, obviously with the vallues i insert when i will run the program:
c[n].nume_proprietar ", " c[n].nume_catel ", " c[n].varsta + " ani , " and on another line
c[n].data_vaccin " " c[n].tip_vaccin
Insert Environment.NewLine where you want to breakline. It is environment specific and provides clarity over "\r\n" hence it's preferred to use.
listBox2.Items.Add(c[n].nume_proprietar + ", " + c[n].nume_catel + ", " + c[n].varsta + " ani ," + Environment.NewLine + c[n].data_vaccin + " " + c[n].tip_vaccin);

Set every element of TextBlockResult to visible when the value is not null

i would like to make each element of textblockresult visible only when the value is not null.
Intent, ROKEntity, ProcedureName etc are strings that are defined as empty and then receive their value from a json :
string intent = "";
if (!string.IsNullOrEmpty(root.XPathSelectElement("//intent").Value))
{
intent = root.XPathSelectElement("//intent").Value;
}
resultToDisplay = "Action: " + intent
+ Environment.NewLine + "Rok Entity: " + ROKEntity
+ Environment.NewLine + "Rok Entity: " + ROKEntity2
+ Environment.NewLine + "Rok Entity: " + ROKEntity3
+ Environment.NewLine + "Procedure Name: " + ProcedureName
+ Environment.NewLine + "Folder Name: " + FolderName
+ Environment.NewLine + "Task Name: " + TaskName
+ Environment.NewLine + "Worker Name: " + WorkerName
+ Environment.NewLine + "Risk Name: " + RiskName
+ Environment.NewLine + "File Name: " + FileName
+ Environment.NewLine + "Workflow Name: " + WorkflowName
+ Environment.NewLine + "Model Name: " + ModelName
+ Environment.NewLine + "Position Name: " + PositionName
+ Environment.NewLine + "Created by: " + CreatedBy
+ Environment.NewLine + "Modified by: " + ModifiedBy
+ Environment.NewLine + "From: " + From
+ Environment.NewLine + "To: " + To
+ Environment.NewLine + "Display Mode: " + DisplayMode
+ Environment.NewLine + "Data Tracking: " + DataTracking
+ Environment.NewLine + "Details: " + Details
+ Environment.NewLine + "Export Format: " + ExportFormat
+ Environment.NewLine + "Ged Files: " + GedFiles
+ Environment.NewLine + "Ged Folders: " + GedFolders
+ Environment.NewLine + "Map: " + Map
+ Environment.NewLine + "Kpi: " + Kpi
+ Environment.NewLine + "Procedure Type: " + ProcedureType
TextBlockResult.Text = resultToDisplay;
ie i want "Action: " + intent to be visible only when the value of intent is not null nor empty, the same for "Rok Entity: " + ROKEntity and so forth ..
For the moment, i have the following xaml that only allows me to set the visibility of the whole lot to collapsed or hidden or visible:
<TextBlock x:Name="TextBlockResult" Visibility="Collapsed" TextWrapping="Wrap" TextAlignment="Center" FontFamily="Segoe UI" FontSize="16" Foreground="Black"><Run Text="Result"/><InlineUIContainer>
What are the different steps to achieve that please ?
if I understand correctly what you are trying to do, you can just skip the elements with empty or null value. So have a method like
private void AddToDisplay(StringBuilder sb, string title, string value)
{
if (!string.IsNullOrEmpty(value))
{
if (sb.Length > 0)
sb.Append(Environment.NewLine);
sb.Append(title);
sb.Append(value);
}
}
then build your result like
var sb = new StringBuilder();
AddToDisplay(sb, "Action: ", intent);
AddToDisplay(sb, "Rok Entity: ", ROKEntity);
.
.
resultToDisplay = sb.ToString();
that way your results show only non-null non-empty elements and optionally hide the entire TextBlock if resultToDisplay is empty after you are done building it

LINQ to Entities does not recognize the method in Aggregate [duplicate]

This question already has answers here:
LINQ to Entities does not recognize the method
(5 answers)
Closed 6 years ago.
I have this LINQ query, I have problem with its Aggregate part:
Adresses = m.RelatedMultipleWorks.Count == 0 ?
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + " + " + m.Road :
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + m.RelatedRoad + " | "
m.RelatedMultipleWorks.Select(z => z.RelatedCity + " + " + z.RelatedCounty + " + " +
z.RelatedDistrict + " + " + z.RelatedNeighborhood + " + " + z.RelatedStreet + " + " + z.RelatedRoad)
.Aggregate((current, next) => current + " | " + next),
And I get this exception.
{"LINQ to Entities does not recognize the method 'System.String
Aggregate[String](System.Collections.Generic.IEnumerable1[System.String],
System.Func3[System.String,System.String,System.String])' method, and
this method cannot be translated into a store expression."}
Why am I getting this exception? How can I get rid of it? Thanks.
Aggregate has not translation to SQL, so Enumerate the results to memory:
.AsEnumerable().Aggregate((current, next) => current + " | " + next);
OR:
.ToList().Aggregate((current, next) => current + " | " + next);
The problem is that the .Aggregate() call is part of the projection to some type, and adding a .ToList() call inside the projection won't work since that projection is translated to sql as a whole. You cannot tell EF to translate half of the projection to SQL, and the other half not. You have to split the projection in two parts, and tell EF to translate the first part, but not the second.
Ok, to solve this. At the moment you have something like this. I can't be specific, and it will be different from your query, since you do not show the full query.
var result = ctx.SomeTable
.Where(...whatever...)
.Select(x => new SomeEntity {
// lots of properties
Adresses = m.RelatedMultipleWorks.Count == 0 ?
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + " + " + m.Road :
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + m.RelatedRoad + " | "
m.RelatedMultipleWorks.Select(z => z.RelatedCity + " + " + z.RelatedCounty + " + " +
z.RelatedDistrict + " + " + z.RelatedNeighborhood + " + " + z.RelatedStreet + " + " + z.RelatedRoad)
.Aggregate((current, next) => current + " | " + next),
// other properties
})
.ToList();
To eliminate the .Aggregate() call in the projection, you need to leave the datasource of the Aggregate intact so L2E can take that data from the database. After that you can apply the .Aggregate() in-memory.
var result = ctx.SomeTable
.Where(...whatever...)
// Change: Do not project to an entity, but to an anonymous type
.Select(x => new {
// lots of properties
// Change: Removed the Aggregate-part
Adresses = m.RelatedMultipleWorks.Count == 0 ?
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + " + " + m.Road :
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + m.RelatedRoad + " | ",
// Added: New property for the aggregate-datasource
RelatedAdresses = m.RelatedMultipleWorks
.Select(z =>
z.RelatedCity + " + " + z.RelatedCounty + " + " + z.RelatedDistrict + " + " +
z.RelatedNeighborhood + " + " + z.RelatedStreet + " + " + z.RelatedRoad
)
// other properties
})
// Added: Call AsEnumerable to stop translating to SQL
.AsEnumerable()
// Added: Project again, fairly simple since all properties are already ok, but now call the Aggregate
.Select(x => new SomeEntity {
// lots of properties
Adresses = Adresses + RelatedAdresses.Aggregate((current, next) => current + " | " + next)
// other properties
})
.ToList();

Browser History -> Database

I want to fetch history from browser as soon as a web site is loaded in the browser. It should fetch clients' browser info and save it in a database so that admin may check out the website that is frequently opened and can do survey on the data.
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n";
Request.Browser is used to identify the browser info . Using this you can get all info of browser
IIS is already storing this information for you. See: http://msdn.microsoft.com/en-us/library/ms525410%28v=vs.90%29.aspx
To make that data useful you will then need to use a Log Analyzer tool.
Google this term for some options to use: iis log analyzer

How to Sort a List in Desending Order by the "Value" and Sort the Duplicates by the "Key"?

I have looked All over the Internet to try and find an example of How to fix the following.
How to Sort a List in Desending Order by the "Value" and Sort the Duplicates by the "Key" ?
Then Print out the Results in the format below.
I have enclosed my code and it works, but the problem happens when there are duplicate values, which occurs when you use SortedList(). I would GREATLY APPRECIATE it if someone could PLEASE Modify this Code or Show me EXACTLY how to do this another way, that is just as quick and efficent.
THANK YOU VERY MUCH in Advance.
BEFORE SORT:
VALUE's, KEY's
sL.Add(1269.63,"white");
sL.Add(1270.36,"orange");
sL.Add(1272.06,"yellow");
sL.Add(1271.50,"cyan");
sL.Add(1272.06,"black");
sL.Add(1274.12,"dodBlue");
sL.Add(1276.02,"blue");
sL.Add(1273.21,"green");
sL.Add(1275.52,"red");
AFTER SORT:
VALUE's, KEY's
sL.Add(1276.02,"blue");
sL.Add(1275.52,"red");
sL.Add(1274.12,"dodBlue");
sL.Add(1273.21,"green");
sL.Add(1272.06,"black");
sL.Add(1272.06,"yellow");
sL.Add(1271.50,"cyan");
sL.Add(1270.36,"orange");
sL.Add(1269.63,"white");
CODE:
SortedList sL = new SortedList();
sL.Add(SMA(8)[0], "white");
sL.Add(SMA(10)[0], "orange");
sL.Add(SMA(13)[0], "yellow");
sL.Add(SMA(20)[0], "cyan");
sL.Add(SMA(30)[0], "black");
sL.Add(SMA(40)[0], "dodBlue");
sL.Add(SMA(50)[0], "blue");
sL.Add(SMA(100)[0], "green");
sL.Add(SMA(200)[0], "red");
Print(" " + " " + sL.GetByIndex(8) + " " + ">=" + " " + sL.GetByIndex(7));
Print("&&" + " " + sL.GetByIndex(7) + " " + ">=" + " " + sL.GetByIndex(6));
Print("&&" + " " + sL.GetByIndex(6) + " " + ">=" + " " + sL.GetByIndex(5));
Print("&&" + " " + sL.GetByIndex(5) + " " + ">=" + " " + sL.GetByIndex(4));
Print("&&" + " " + sL.GetByIndex(4) + " " + ">=" + " " + sL.GetByIndex(3));
Print("&&" + " " + sL.GetByIndex(3) + " " + ">=" + " " + sL.GetByIndex(2));
Print("&&" + " " + sL.GetByIndex(2) + " " + ">=" + " " + sL.GetByIndex(1));
Print("&&" + " " + sL.GetByIndex(1) + " " + ">=" + " " + sL.GetByIndex(0));
PRINT OUT RESULTS:
blue >= red ;
&& red >= dodBlue ;
&& dodBlue >= green ;
&& green >= yellow ;
&& yellow >= black ;
&& black >= cyan ;
&& cyan >= orange ;
&& orange >= white ;
Why don't you use LINQ for this? OrderBy() and Distinct() methods will help you.
You can do this with LINQ as shown below:
Dictionary<int, string> lst = new Dictionary<int,string>(10);
lst.Add(7, "MAX");
lst.Add(2, "A");
lst.Add(1, "A");
lst.Add(8, "0");
Dictionary<int, string> newList = (lst.OrderBy(x => x.Value).ThenBy(x => x.Key)).ToDictionary(x => x.Key,x=>x.Value);

Categories

Resources