|
List<string> periods = new List<string>();
periods.Add("2010 FYA");
periods.Add("2011 FYA");
periods.Add("2012 FYA");
periods.Add("2013 FYA");
periods.Add("1Q 2014A");
periods.Add("2Q 2014A");
periods.Add("3Q 2014A");
periods.Add("4Q 2014A");
periods.Add("2014 FYA");
var xx = periods.Where(a => a.ToString() == "2010 FYA" && a.ToString() == "3Q 2014A").ToList();
IN LINQ there is no any between operator. so tell me how to use LINQ to extract data between two period.
i need to extract all data from period 2010 FYA to 3Q 2014A using LINQ. please show me how to achieve with LINQ. thanks
|
|
|
|
|
You will need to convert your "date strings" to actual DateTime values - which won't be trivial, given they don't appear to have a consistent format and that means it's likely there are a lot of "oddities" in there - then you can use
.Where( d => d >= startdate && d <= endDate); But you will get absolutely nowhere trying to process them as strings: they are always sorted by the first character difference encountered in the two strings being compared.
"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!
|
|
|
|
|
Where(a => a.ToString() == "2010 FYA" && a.ToString() == "3Q 2014A")
The item in question cannot be equal to both of those literal strings. It can only be one or the other. And you should not need to call ToString on an item that is already a string:
Where(a => a == "2010 FYA" || a == "3Q 2014A")
|
|
|
|
|
i have to put a condition in where clause as a result all periods between two periods will be fetched....how to achieve with LINQ ?
|
|
|
|
|
You need to convert all those strings to proper Date values as suggested by @OriginalGriff earlier. As it stands there is no way to do such a comparison with them.
|
|
|
|
|
Bogus data. Your DBA should be retired.
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'm with Gerry on this, garbage data in the database. I would add another field to the table and convert the garbage to datetime values ON THE WAY IN. Use the garbage for display only.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
I have done the job this way and it worked.
int startIndex = periods.IndexOf("2011 FYA");
int endIndex = periods.IndexOf("3Q 2014A");
var anybetween = periods.Skip(startIndex).Take(((endIndex - startIndex) + 1)).ToList();
|
|
|
|
|
That method depends on the text string and the sort order, the text string is going to change when they add/subtract a period and changing the order of the data totally screws up you query.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
"Q A" is not dates. "Between" should ignore it, and as intended it does.
Or as the others said, your database is crap. Learn normalization and you wont have these problems ever again. Or don't, is all the same to me.
Homework? Welcome, as long as you don't ask for a solution but a hand or hint. But given your history and your last slosh of questions; I'm not here to answer, but to make fun. You treating this board as a cheap source of labor which it isn't. If I work for you, you pay.
If you reply to my messages, you agree to pay for them. Any questions? (hahaha, you'd pay for them, if you would, including this one!)
I'll inform you of my rate after you reply.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
i have created sample small program to learn how to report progress from Async function but not getting expected result. here is my code. please tell me what is my mistake in the code.
i am trying to write in textbox from async function which is not working and also not throwing any error.
public async Task<int> Getdata(int i)
{
IProgress<string> progress = new Progress<string>(str =>
{
textBox1.Text = str;
});
await Task.Delay(90000);
progress.Report("Task completed "+i.ToString());
return 10;
}
private async void btnTask1_Click(object sender, EventArgs e)
{
List<Task<int>> tasks = new List<Task<int>>();
for (int i = 0; i < 5; i++)
{
tasks.Add(Getdata(i + 1));
}
var result = await Task.WhenAll(tasks);
}
|
|
|
|
|
You haven't described your expected result, nor told us what "not working" means.
Bear in mind your task will all complete at roughly the same time, 90 seconds after you start them. You will only notice a single update to the textbox.
It's generally better to avoid async void methods[^]. And I'd be inclined to create a single Progress<T> instance and pass it in to the Getdata method.
public async Task GetData(int i, IProgress<string> progress)
{
progress.Report($"Task created {i}");
await Task.Delay(60000 + i * 1000);
progress.Report($"Task completed {i}");
}
private async Task Run(IProgress<string> progress)
{
List<Task> tasks = new List<Task>();
for (int i = 0; i < 5; i++)
{
tasks.Add(GetData(i + 1, progress));
}
await Task.WhenAll(tasks);
}
private void btnTask1_Click(object sender, EventArgs e)
{
IProgress<string> progress = new Progress<string>(str => textBox1.Text = str);
_ = Run(progress);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks for your sample code but i am using .Net v4.5.2. so i change the code this way as a result to compile. Sir please have a look at converted code and tell me is it ok ?
private async void button1_Click(object sender, EventArgs e)
{
IProgress<string> progress = new Progress<string>(str =>
{
textBox1.Text += str+System.Environment.NewLine;
});
await Run(progress);
}
public async Task GetData(int i, IProgress<string> progress)
{
progress.Report("Task created "+i.ToString());
await Task.Delay(2000);
progress.Report("Task completed "+i.ToString());
}
private async Task Run(IProgress<string> progress)
{
List<Task> tasks = new List<Task>();
for (int i = 0; i < 5; i++)
{
tasks.Add(GetData(i + 1, progress));
}
await Task.WhenAll(tasks);
}
modified 30-Apr-21 4:21am.
|
|
|
|
|
As I said, avoid async void methods. If you can't use the discard, either assign the task to a dummy variable, or ignore the compiler warning.
private async void button1_Click(object sender, EventArgs e)
{
IProgress<string> progress = new Progress<string>(str => textBox1.Text += str + System.Environment.NewLine);
Run(progress);
} Beyond that, you tell me: does it work? Does it do what you expect?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sir i am not familiar with this syntax _ = Run(progress); means _ = func()?
what kind of syntax is it and what it means. i call it like await Run(progress);
|
|
|
|
|
The _ is a place holder for a throw-away variable.
It just means whatever the Run() call returns is ignored, or thrown away. It saves you from having to define a variable of the correct type to hold a value you're not going to use any way.
|
|
|
|
|
"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!
"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 know the program not throwing error and compile properly. the problem is i am expecting that when program will run then 1,2,3....5 should be showing in textbox but it is not occurring properly. sir if you run the code then you will see 1,2,3 not showing in textbox one after one after delay. so i confused when program not behaving properly. please help me and tell me what mistake i made there for which digits not showing in textbox one after one gradually. thanks
|
|
|
|
|
Why on earth would you expect it to do that?
You are starting 5 identical tasks, each of which show a message in the same box, wait the same amount of time, and clear it.
Since all the tasks are the same, and they all output to the same textbox, all you will ever see is the final output: whichever task finished last - and that'll be random because they all have the same priority and run in different threads.
Why?
Because they all wait the same time: so after the same 90 seconds they all try to run and all update the same text box. Last one "wins" and that's all you see.
"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!
|
|
|
|
|
Thank you sir pointing the problem.
|
|
|
|
|
A BGW makes that simple. Even got examples on MSDN.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I am iterating in 2509693 data in for loop which is taking long time. i am using VS2013 and .Net v4.5.2
so see my code and suggest some approach or changes for my existing code which speed up execution of my for loop.
This ds.Tables[1] has 2509693 data please guide me how to restructure & speed up below code. Thanks
public static List<ElementHierarchy> GetElementHierarchy(DataSet ds)
{
List<ElementHierarchy> _ElmHierarchy = new List<ElementHierarchy>();
string StrPrevDisplayInCSM = "", DisplayInCSM = "", Section = "", LineItem = "", LastGroupName = "", BGColor="",
BlueMatrix1stElementFormulaText = "", Type = "", Period = "", EarningsType = "", ParentGroup = "", HeadingSubheading = "", Box="";
int row = 6, EarningID = 0, LineItemID = 0, BMID = 0, ID = 0, ParentID=0;
bool IsNextElementGroup = false;
List<ListOfSection> lstData = new List<ListOfSection>();
bool IsGreenHeader = false;
for (int p = 0; p <= ds.Tables[1].Rows.Count - 1; p++)
{
ID = Convert.ToInt32(ds.Tables[1].Rows[p]["ID"].ToString());
ParentID = Convert.ToInt32(ds.Tables[1].Rows[p]["ParentID"].ToString());
EarningID = 0;
Section = (ds.Tables[1].Rows[p]["Section"] == DBNull.Value ? "" : ds.Tables[1].Rows[p]["Section"].ToString());
LineItem = (ds.Tables[1].Rows[p]["LineItem"] == DBNull.Value ? "" : ds.Tables[1].Rows[p]["LineItem"].ToString());
DisplayInCSM = ds.Tables[1].Rows[p]["DisplayInCSM"].ToString();
Type = ds.Tables[1].Rows[p]["Type"].ToString();
BlueMatrix1stElementFormulaText = (ds.Tables[1].Rows[p]["BlueMatrix1stElementFormulaText"] == null
? "" : ds.Tables[1].Rows[p]["BlueMatrix1stElementFormulaText"].ToString());
Period = (ds.Tables[1].Rows[p]["Period"] == DBNull.Value ? "" : ds.Tables[1].Rows[p]["Period"].ToString());
HeadingSubheading = (ds.Tables[1].Rows[p]["HeadingSubheading"] == null ? "" : ds.Tables[1].Rows[p]["HeadingSubheading"].ToString());
Box = (ds.Tables[1].Rows[p]["Box"] == DBNull.Value ? "" : ds.Tables[1].Rows[p]["Box"].ToString());
LineItemID = Convert.ToInt32(ds.Tables[1].Rows[p]["LineItemID"].ToString());
BMID = Convert.ToInt16(ds.Tables[1].Rows[p]["BMID"].ToString());
BGColor = (ds.Tables[1].Rows[p]["BGColor"] == null ? "" : ds.Tables[1].Rows[p]["BGColor"].ToString());
if (BGColor.Contains("ff003300"))
{
IsGreenHeader = true;
}
else
{
IsGreenHeader = false;
}
if (StrPrevDisplayInCSM != "" && StrPrevDisplayInCSM != DisplayInCSM && (Type == "LINEITEM" || Type=="BM"))
{
row++;
}
if (Type == "GROUP")
{
if (IsNextElementGroup)
{
row++;
}
else if (p > 0 && !IsNextElementGroup)
{
row++;
if (p > 0 && HeadingSubheading=="H")
{
row++;
}
if (p > 0 && HeadingSubheading == "S")
{
row++;
}
}
else if (p > 0 && IsGreenHeader)
{
row++;
}
else if (p > 0 && ds.Tables[1].AsEnumerable().Any(a => a.Field<int>("ParentID") == ID && a.Field<string>("Type") == "GROUP"))
{
row++;
}
ParentGroup = DisplayInCSM;
if (HeadingSubheading != "")
{
if (HeadingSubheading == "H")
{
if (Box != "Y")
{
}
}
}
if(IsGreenHeader)
{
row++;
}
else if (ds.Tables[1].AsEnumerable().Any(a => a.Field<int>("ParentID") == ID && a.Field<string>("Type")=="GROUP"))
{
row++;
}
IsNextElementGroup = true;
}
else if (Type == "LINEITEM")
{
if (!lstData.Any(a =>
a.Section == Section
&& a.LineItem == LineItem
&& a.Parent == ParentGroup
&& a.DisplayINCSM == DisplayInCSM
&& a.EarningsID == EarningID
&& a.EarningsType == EarningsType
&& a.Period == Period
))
{
if (!_ElmHierarchy.Any(z => z.RowIndex == row))
{
_ElmHierarchy.Add(new ElementHierarchy
{
ID=ID,
ParentID=ParentID,
RowIndex = row,
Section = Section,
Lineitem = LineItem,
Type = "LI",
DisplayInCSM = DisplayInCSM,
BMFormula = "",
LineitemID = LineItemID,
BMID = 0
});
}
lstData.Add(new ListOfSection
{
Section = Section,
LineItem = LineItem,
DisplayINCSM = DisplayInCSM,
Parent = ParentGroup,
EarningsID = EarningID,
EarningsType = EarningsType,
Period = Period
});
}
IsNextElementGroup = false;
IsGreenHeader = false;
}
else if (Type == "BM")
{
IsNextElementGroup = false;
IsGreenHeader = false;
if (!lstData.Any(a =>
a.Section == Section
&& a.LineItem == LineItem
&& a.Parent == ParentGroup
&& a.DisplayINCSM == DisplayInCSM
&& a.EarningsID == EarningID
&& a.EarningsType == EarningsType
&& a.Period == Period
))
{
if (!_ElmHierarchy.Any(z => z.RowIndex == row))
{
_ElmHierarchy.Add(new ElementHierarchy
{
ID = ID,
ParentID = ParentID,
RowIndex = row,
Section = Section,
Lineitem = LineItem,
Type = "BM",
DisplayInCSM = DisplayInCSM,
BMFormula = BlueMatrix1stElementFormulaText,
LineitemID = 0,
BMID = BMID
});
}
lstData.Add(new ListOfSection
{
Section = Section,
LineItem = LineItem,
DisplayINCSM = DisplayInCSM,
Parent = ParentGroup,
EarningsID = EarningID,
EarningsType = EarningsType,
Period = Period
});
}
}
StrPrevDisplayInCSM = DisplayInCSM;
}
return _ElmHierarchy;
}
public class ListOfSection
{
public string Parent { get; set; }
public int EarningsID { get; set; }
public string EarningsType { get; set; }
public string Section { get; set; }
public string LineItem { get; set; }
public string DisplayINCSM { get; set; }
public string Period { get; set; }
}
|
|
|
|
|
Which lines are taking the longest to execute?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I have no idea what you are trying to do or why you need to update so many items. However, Stop calling ToString on items that are already strings. And look at the following line (just one example of a few):
ID = Convert.ToInt32(ds.Tables[1].Rows[p]["ID"].ToString());
Why are you converting a value to a string, just so you can convert it to an integer?
But you need to understand that processing just over 2.5 million items will take some time. The laws of physics are immutable.
|
|
|
|
|
Richard is right:
Quote: Which lines are taking the longest to execute?
And so is Richard:
Quote: you need to understand that processing just over 2.5 million items will take some time.
The first part of any speed improvement exercise is to find out what you have: and that means timing your code to find out what takes a "long time" and what is "quick" - there is no point in trying to squeeze improvements out of fast code because that will make marginal difference; the slow code is teh bit that needs speeding up.
So start with the Stopwatch Class (System.Diagnostics) | Microsoft Docs[^] and start finding out where that code runs quick, and where it runs slow.
We can't do that for you: we don't have any access to your data, and it's likely to be very relevant to any speed analysis.
When you have that, you also have a metric for much improvement you are making as you go.
It's possible that you might be able to multithread it, to spread the load out over the whole processor - but that's not as simple as it may sound: although thinks like Parallel.Foreach exist, using threading blindly is as likely to slow down your whole computer as speed up processing, particularly when you start dealing with 2,000,000 items each of which probably don't take much time so the additional thread setup / processing overhead starts to overweigh the actual task.
You may get some good "throughput" improvements by splitting the for loop into a small number N of threads, each of which does 1/N th of the total task. But remember: List and DataTable are not considered thread safe, so you'll probably need to think very carefully before you start coding that if you don't want really hard to spot and track down data bugs.
As a general rule, I wouldn't recommend trying to use more threads than you have cores ...
"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!
|
|
|
|
|