Using InputStr (Formula) I want to find the string first match the exact string from string1 and then followed by get values in string2 as order using Inputstr Formula.
i want to use the above calculation using C# with LINQ.
So the Final Output would be as below.
double Result = 0;
Result = 86 + 95 - 150 + 96 - 98; (As per Formula, we need to find and calculate the results)
Result = 525 (Final)..
How to write C# code using LINQ.?
I am geting confused whether we need to go for LINQ or C#.
Can anybody give sample idea to start the above logic using code..?
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
it seems the final result you calculate is wrong. it should be 29.
Please check the following code:
static string result = "";
static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
string InputStr = "Mark1+Mark2-AddlMark+Mark3-Mark4";
var total = Caculate(InputStr);
Response.Write(total);
}
protected int Caculate( string InputStr) {
int AddlMark = 150;
string String1 = "Mark1-Mark2-Mark3-Mark4-Mark5-Mark6"; // and its values is below
string String2 = "86-95-96-98-99-100"; //(Mark1 - Mark2 - Mark3 - Mark4 - Mark5 - Mark6)
Dictionary<string, int> direct = new Dictionary<string, int>();
string[] string1 = String1.Split('-');
string[] string2 = String2.Split('-');
for (int i = 0; i < string1.Length; i++)
{
direct.Add(string1[i], Convert.ToInt16(string2[i]));
}
direct.Add("AddlMark", AddlMark);
foreach (var d in direct)
{
string orinal = InputStr.ToLower();
string match = d.Key.ToLower();
var flag = orinal.IndexOf(match);
if (i == 1)
{
if (flag > 0)
result = result.Replace(match, d.Value.ToString());
}
if (i == 0)
{
if (flag >= 0)
{
result = orinal.Replace(match, d.Value.ToString());
}
i = 1;
}
}
int total = 0;
var re = result.Replace("-", "+-");
string[] res = re.Split('+');
foreach (string st in res)
{
total += Convert.ToInt16(st);
}
return total;
}
Output:
Best regards
Cathy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Getting Error and result not coming of my final modified code
Error line :
total += Convert.ToInt64(st);
Final Code
protected void Button8_Click(object sender, EventArgs e)
{
string InputStr = "DIM H + QUT OF PIP DIM";
var total = Caculate(InputStr);
Response.Write(total);
}
protected int Caculate(string InputStr)
{
double AddlMark = 381;
string String1 = "SPT TGA-DIM H-DIM L-PIP DIM-SPT PIP CLS"; // and its values is below
string String2 = "S6-100-450-30-CS"; //
Dictionary<string, string> direct = new Dictionary<string, string>();
string[] string1 = String1.Split('-');
string[] string2 = String2.Split('-');
for (int i = 0; i < string1.Length; i++)
{
direct.Add(string1[i], string2[i]);
}
direct.Add("QUT OF PIP DIM", Convert.ToString(AddlMark));
foreach (var d in direct)
{
string orinal = InputStr.ToUpper();
string match = d.Key.ToUpper();
var flag = orinal.IndexOf(match);
if (i == 1)
{
if (flag > 0)
result = result.Replace(match, d.Value.ToString());
}
if (i == 0)
{
if (flag >= 0)
{
result = orinal.Replace(match, d.Value.ToString());
}
i = 1;
}
}
int total = 0;
var re = result.Replace("-", "+-");
string[] res = re.Split('+');
foreach (string st in res)
{
total += Convert.ToInt64(st);
}
return total;
}
}
What is the problem over here and where it is...????
After debug your code step by step. I found that you need to change the name in your ‘InputStr’ Formula string.
PIP DIM——>PIP DIM Total
Code as below:
protected void Button1_Click(object sender, EventArgs e)
{
string InputStr = "DIM H + QUT OF PIP DIM";
var total = Caculate(InputStr);
Response.Write(total);
}
protected int Caculate(string InputStr)
{
double AddlMark = 381;
// string String1 = "SPT TGA-DIM H-DIM L-PIP DIM-SPT PIP CLS"; // and its values is below
1. string String1 = "SPT TGA-DIM H-DIM L-PIP DIM Total-SPT PIP CLS"; // and its values is below
string String2 = "S6-100-450-30-CS"; //
Dictionary<string, string> direct = new Dictionary<string, string>();
string[] string1 = String1.Split('-');
string[] string2 = String2.Split('-');
for (int i = 0; i < string1.Length; i++)
{
direct.Add(string1[i], string2[i]);
}
direct.Add("QUT OF PIP DIM", Convert.ToString(AddlMark));
foreach (var d in direct)
{
string orinal = InputStr.ToUpper();
string match = d.Key.ToUpper();
var flag = orinal.IndexOf(match);
if (s == 1)
{
if (flag > 0)
result = result.Replace(match, d.Value.ToString());
}
if (s == 0)
{
if (flag >= 0)
{
result = orinal.Replace(match, d.Value.ToString());
s = 1;
}
}
}
int total = 0;
var re = result.Replace("-", "+-");
string[] res = re.Split('+');
foreach (string st in res)
{
2. total += Convert.ToInt16(st);
}
return total;
}
Output:
Best regards
Cathy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
1. string String1 = "SPT TGA-DIM H-DIM L-PIP DIM Total-SPT PIP CLS"; // and its values is below
I understood your solution.
But, This value coming from table. so we cannot change.
instead of changing we need to find some other logic...?????
i'm struggling...????
also i tried your code. but other error is coming.
Error : Input string is not in a correct format
protected void Button8_Click(object sender, EventArgs e)
{
string InputStr = "DIM H + QUT OF PIP DIM";
var total = Caculate(InputStr);
Response.Write(total);
}
protected int Caculate(string InputStr)
{
double AddlMark = 381;
string String1 = "SPT TGA-DIM H-DIM L-PIP DIM Total-SPT PIP CLS"; // and its values is below
string String2 = "S6-100-450-30-CS"; //
Dictionary<string, string> direct = new Dictionary<string, string>();
string[] string1 = String1.Split('-');
string[] string2 = String2.Split('-');
for (int i = 0; i < string1.Length; i++)
{
direct.Add(string1[i], string2[i]);
}
direct.Add("QUT OF PIP DIM", Convert.ToString(AddlMark));
foreach (var d in direct)
{
string orinal = InputStr.ToUpper();
string match = d.Key.ToUpper();
var flag = orinal.IndexOf(match);
if (s == 1)
{
if (flag > 0)
result = result.Replace(match, d.Value.ToString());
}
if (s == 0)
{
if (flag >= 0)
{
result = orinal.Replace(match, d.Value.ToString());
}
s = 1;
}
}
int total = 0;
var re = result.Replace("-", "+-");
string[] res = re.Split('+');
foreach (string st in res)
{
total += Convert.ToInt16(st); // Error : Input string is not a corect format.
}
return total;
}
Also, i want to use PIP DIM only. not required PIP DIM Total.
string String1 = "SPT TGA-DIM H-DIM L-PIP DIM Total-SPT PIP CLS"; // and its values is below
protected void Button1_Click(object sender, EventArgs e)
{
string InputStr = "DIM H + QUT OF PIP DIM";
var total = Caculate(InputStr);
Response.Write(total);
}
protected int Caculate(string InputStr)
{
double AddlMark = 381;
string String1 = "SPT TGA-DIM H-DIM L-PIP DIM-SPT PIP CLS"; // and its values is below
string String2 = "S6-100-450-30-CS"; //
Dictionary<string, string> direct = new Dictionary<string, string>();
string[] string1 = String1.Split('-');
string[] string2 = String2.Split('-');
string[] string3 = InputStr.Replace(" ",String.Empty).Replace("-", "+-").Split('+');
for (int i = 0; i < string1.Length; i++)
{
direct.Add(string1[i], string2[i]);
}
direct.Add("QUT OF PIP DIM", Convert.ToString(AddlMark));
foreach (string s1 in string3)
{
foreach (var d in direct)
{
string orinal = InputStr.Replace(" ", String.Empty).ToUpper();
string match = d.Key.Replace(" ", String.Empty).ToUpper();
//var flag = ;
if (s == 1)
{
if (s1.TrimStart('-')== match)
result = result.Replace(match, d.Value.ToString());
}
if (s == 0)
{
if (s1.TrimStart('-') == match)
{
result = orinal.Replace(match, d.Value.ToString());
s = 1;
}
}
}
}
int total = 0;
var re = result.Replace("-", "+-");
string[] res = re.Split('+');
foreach (string st in res)
{
total += Convert.ToInt16(st);
}
return total;
}
Best regards
Cathy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
209 Points
701 Posts
C# or LINQ - How to Find Exact Index and caluculate values.
Sep 07, 2017 09:54 AM|gani7787|LINK
How to Find Exact Index and caluculate values.
My requirement is below code and logic :
int AddlMark = 150;
string InputStr = "Mark1 + Mark2 - AddlMark + Mark3" - Mark4"; (Formula)
string String1 = "Mark1-Mark2-Mark3-Mark4-Mark5-Mark6"; // and its values is below
string String2 = "86-95-96-98-99-100"; (Mark1-Mark2-Mark3-Mark4-Mark5-Mark6)
Using InputStr (Formula) I want to find the string first match the exact string from string1 and then followed by get values in string2 as order using Inputstr Formula.
i want to use the above calculation using C# with LINQ.
So the Final Output would be as below.
double Result = 0;
Result = 86 + 95 - 150 + 96 - 98; (As per Formula, we need to find and calculate the results)
Result = 525 (Final)..
How to write C# code using LINQ.?
I am geting confused whether we need to go for LINQ or C#.
Can anybody give sample idea to start the above logic using code..?
Participant
1630 Points
932 Posts
Re: C# or LINQ - How to Find Exact Index and caluculate values.
Sep 07, 2017 11:54 PM|PaulTheSmith|LINK
Why does it have to be linq? Is that a part of the assignment?
Star
8670 Points
2882 Posts
Re: C# or LINQ - How to Find Exact Index and caluculate values.
Sep 08, 2017 07:49 AM|Cathy Zou|LINK
Hi
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Star
8670 Points
2882 Posts
Re: C# or LINQ - How to Find Exact Index and caluculate values.
Sep 08, 2017 09:45 AM|Cathy Zou|LINK
Hi gani7787,
it seems the final result you calculate is wrong. it should be 29.
Please check the following code:
Output:
Best regards
Cathy
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
209 Points
701 Posts
Re: C# or LINQ - How to Find Exact Index and caluculate values.
Sep 11, 2017 12:28 PM|gani7787|LINK
Thanks for your excellent coding.
Getting Error and result not coming of my final modified code
Error line :
Final Code
What is the problem over here and where it is...????
Star
8670 Points
2882 Posts
Re: C# or LINQ - How to Find Exact Index and caluculate values.
Sep 12, 2017 02:35 AM|Cathy Zou|LINK
Hi gani7787,
After debug your code step by step. I found that you need to change the name in your ‘InputStr’ Formula string.
PIP DIM——>PIP DIM Total
Code as below:
Output:
Best regards
Cathy
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
209 Points
701 Posts
Re: C# or LINQ - How to Find Exact Index and caluculate values.
Sep 12, 2017 04:32 AM|gani7787|LINK
1. string String1 = "SPT TGA-DIM H-DIM L-PIP DIM Total-SPT PIP CLS"; // and its values is below
I understood your solution.
But, This value coming from table. so we cannot change.
instead of changing we need to find some other logic...?????
i'm struggling...????
also i tried your code. but other error is coming.
Error : Input string is not in a correct format
Also, i want to use PIP DIM only. not required PIP DIM Total.
pls. give suggestion...
Star
8670 Points
2882 Posts
Re: C# or LINQ - How to Find Exact Index and caluculate values.
Sep 13, 2017 02:11 AM|Cathy Zou|LINK
Hi gani7787,
Using the following code:
Best regards
Cathy
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
209 Points
701 Posts
Re: C# or LINQ - How to Find Exact Index and caluculate values.
Sep 14, 2017 04:18 AM|gani7787|LINK
Thanks....Excellent...