AppendAllLines alternative solution - c#

I am trying to write a txt file from C# as follows:
File.WriteAllText("important.txt", Convert.ToString(c));
File.AppendAllLines("important.txt", (from r in rec
select r.name + " " + r.num1 + " " + r.num2 + " " + r.mult + " " + r.rel).ToArray());
I am getting error AppendAllLInes not found for system.IO.File any alternative approach or how can I include AppendAllLInes

Write all at once.
var part1 = Convert.ToString(c);
var part2 = String.Join(Environment.NewLine,
rec.Select(r => r.name + " " + r.num1 + " " + r.num2 + " " + r.mult + " " + r.rel)
.ToArray());
System.IO.File.WriteAllText("important.txt", part1 + part2);
You could also use WriteAllLines in 3.5:
var allLines = new []{Convert.ToString(c)}
.Concat(rec.Select(r => r.name + " " + r.num1 + " " + r.num2 + " " + r.mult + " " + r.rel))
.ToArray();
System.IO.File.WriteAllLines("important.txt", allLines);

Related

2 lines in the Debug.Log result 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

Sorting collection using OrderBy not working

I am trying to sort my collection using lambda according to (in this case "Amount") but also tried other fields. when I output the collection using foreach it is not sorted according to the field
Accounts? ac = await Client.SendRequest<Accounts>(tk.access_token, Method.GET, "/UploadedAccounts", "");
ac.value.OrderBy(x => x.Amount);
ac.value.RemoveAll(x => x.Year.ToString() != YearBox.Text);
foreach (var a in ac.value) {
ListViewBox.Items.Add(a.CompanyLegalName + " / " + a.Year + "-" + a.Month + " / " + a.AccountName + " / " + a.AccountCode + " / " + a.AccountType + " / " + a.Amount + " / " + a.Dimension , 0);
}
I have also tried to do the sorting after the 'RemoveAll'
what am I doing wrong?
.OrderBy(x => x.Amount) sorts the collection and returns sorted Accounts. You need to do foreach on the list which is return by .OrderBy(x => x.Amount)
Like,
foreach (var a in ac.value.OrderBy(x => x.Amount))
{
ListViewBox.Items.Add(a.CompanyLegalName + " / " + a.Year + "-" + a.Month + " / " + a.AccountName + " / " + a.AccountCode + " / " + a.AccountType + " / " + a.Amount + " / " + a.Dimension , 0);
}
The issue is that OrderBy LINQ extension is returning sorted enumerable. It is not modifying the original collection.

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();

C# Microsoft SQL

I want to write that two strbldrSQL.Append in single strbldrSQL.
Because Im inserting records in 2 DataSets dsorderHeader and dsorderDetail respectively.
if (strbldrSQL != null) strbldrSQL.Clear();
else strbldrSQL = new StringBuilder();
strbldrSQL.Append(#" SELECT ORDHEDR01.* FROM ORDHEDR01 "
+ " WHERE ORDHEDR01.COMPANY = " + argintCompany + " "
+ " AND ORDHEDR01.WHSE = " + argintWarehouse + " "
+ " AND ORDHEDR01.ORDNUM = " + arglngOrderNumber + " "
);
dsOrderHeader = DAL.pfGetDataSet(ref argobjMnCnnCls, strbldrSQL.ToString(), true);
if (dsOrderHeader == null) return CNS.gcUpdateFailed + ";" + "Unable to get data";
strbldrSQL.Clear();
strbldrSQL.Append(#" SELECT ORDDETL02.* FROM ORDDETL02 "
+ " WHERE ORDDETL02.COMPANY = " + argintCompany + " "
+ " AND ORDDETL02.WHSE = " + argintWarehouse + " "
+ " AND ORDDETL02.ORDNUM = " + lngOrderNumber + " "
);
dsOrderDetail = DAL.pfGetDataSet(ref argobjMnCnnCls, strbldrSQL.ToString(), true);
if (dsOrderDetail == null) return CNS.gcUpdateFailed + ";" + "Unable to get data";

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