Jitesh Dammani Jitesh Dammani - 3 months ago 14
ASP.NET (C#) Question

Getting a format error, cannot convert system.double to system.string

Question1#
While executing the below code its giving me a format error. System.double cannot be convert into System.string.

var PiePriorityCount = dt.AsEnumerable()
.GroupBy(item => item.Field<string>("Priority"))
.Select(item => new { Priority = item.Key, TicketCount = item.Count() });


dtPriorityCount.Columns.Add("Priority", typeof(string));
dtPriorityCount.Columns.Add("Count", typeof(int));
lblStatus.Visible = true;
foreach (var itm in PiePriorityCount)
{
//lblStatus.Text = "Z" + "->" + itm.TicketCount;
DataRow dr = dtPriorityCount.NewRow();
dr["Priority"] = "P" + " " +itm.Priority;
dr["Count"] = itm.TicketCount;
dtPriorityCount.Rows.Add(dr);
}


While executing the above code its giving me a format error. System.double cannot be convert into System.string.

Question 2# Got the Answer
In the Sting, LOG, contains Status on the first line and Owned Group on the second line and Assigned Group on the third. So i want the sub-string to be After status till the end of the line. i.e. Assigned (1)

And they can be shuffled in order. Not necessary that the first one will be status, it can be owned group or assigned group.

string LOG = "Status: Assigned (1) \n Owned Group: Jitesh - poiuyt \n Assigned Group: Jitesh - QWERTY";

string tb = "Status: ";
string tb1 = "Assigned Group: ";

int lenght = tb.Length;
int a = LOG.IndexOf(tb) + lenght;

int lenght1 = tb1.Length;
int a1 = LOG.IndexOf(tb1) + lenght1;

var status = LOG.Substring(a,'\n');
var status1 = LOG.Substring(a1, '\n');


Expected Output :

status = Assigned (1)

status1 = Jitesh - QWERTY

Current Output :

status = Assigned (

status1 = Jitesh - Q

This is what i am expecting and this is what i am getting.

Answer

You can use Split twice:

  • with \n to get Status: Assigned (1) and Assigned Group: Jitesh - QWERTY, and then
  • with : and trim the values.

C# demo:

var LOG = "Status: Assigned (1) \n Owned Group: Jitesh - poiuyt \n Assigned Group: Jitesh - QWERTY";
var res = LOG.Split('\n')
    .Where(p => p.Trim().StartsWith("Status:") || p.Trim().StartsWith("Assigned Group:"))
    .Select(n => n.Split(':').LastOrDefault().Trim())
    .ToList();

var status = res[0];
var status1 = res[1];

You will have 2 values there, the first is status, the second one is status1.

Another way to get 2 variables if their order can be random:

var status = LOG.Split('\n')
    .Where(p => p.Trim().StartsWith("Status:"))
    .Select(n => n.Split(':').LastOrDefault().Trim())
    .FirstOrDefault();
var status1 = LOG.Split('\n')
    .Where(p => p.Trim().StartsWith("Assigned Group:"))
    .Select(n => n.Split(':').LastOrDefault().Trim())
    .FirstOrDefault();

See another demo