2

In MySQL table I need check this condition using C# in aspnet on DataSet :

if (A < 0.1 && B > 1 && (C >= 1 && C <= 4))

When all conditions are true I need to print the value on aspx page.

I have tried without success this code :

Double[] A = dt.Rows[0]["A"].Split(',');
Double[] B = dt.Rows[0]["B"].Split(',');
Double[] C = dt.Rows[0]["C"].Split(',');

long AC = A.Where(x => x < 0.1).Count();
long BC = B.Where(x => x > 1).Count();
long CC = C.Where(x => x >= 1 && x <= 4).Count();

if((AC == 1) && (BC == 1) && (CC == 1)
{
    //Print
}

But the error is :

C# 'object' does not contain a definition for “Split”

My table below and column A, B and C are setting as Decimal 10,2 value.

+---+-------+------+
| A | B     | C    |
+---+-------+------+
| 0 | 16,11 | 3,08 |
+---+-------+------+

How can I resolve this?

Edit #01

I can't print the value ...

Double[] A = dt.Rows[0]["A"].ToString().Split(',').Select(double.Parse).ToArray(); 
string str = A.ToString();  
Response.Write(str);

Edit #02

This is my table ( first 10 rows ) :

+------+------+------+
| A    | B    | C    |
+------+------+------+
| 0,59 | 1,54 | 1    |
| 6,77 | 0,13 | 1,3  |
| 1    | 1,26 | 1    |
| 0,67 | 0,18 | 1,5  |
| 1    | 0,13 | 0,1  |
| 1    | 0,38 | 0,08 |
| 0,5  | 0,17 | 4    |
| 6,73 | 0,29 | 15   |
| 0    | 16,1 | 3,8  |
| 0,13 | 0,8  | 8    |
+------+------+------+

I expected this result of printing :

A >>> 0,59 6,77 1 0,67 1 1 0,5 6,73 0 0,13

B >>> 1,54 0,13 1,26 ...

C >>> 1 1,3 1 ...

But instead in output I have :

A >>> 1 0

B >>> 1 0

C >>> 1 0

I use your suggestion on this code :

if (ds.Tables.Count > 0)
{
    dt = ds.Tables[0];

    foreach (DataTable table in ds.Tables)
    {
        foreach (DataRow row in table.Rows)
        {
            Double[] A = dt.Rows[0]["A"].ToString().Split(',').Select(double.Parse).ToArray();
            Double[] B = dt.Rows[0]["B"].ToString().Split(',').Select(double.Parse).ToArray();
            Double[] C = dt.Rows[0]["C"].ToString().Split(',').Select(double.Parse).ToArray();

            Response.Write("A >>> " + String.Join(" ", A.Select(a => a.ToString()).ToArray()) + "<br /><br />");

            Response.Write("B >>> " + String.Join(" ", B.Select(b => b.ToString()).ToArray()) + "<br /><br />");

            Response.Write("C >>> " + String.Join(" ", C.Select(c => c.ToString()).ToArray()) + "<br /><br />");
        }
    }
}

Edit #03

Error:

The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments

Code

if (ds.Tables.Count > 0)
{
    dt = ds.Tables[0];

    var rows = dt.Rows.OfType<DataRow>().ToArray();

    Response.Write("A >>> " + string.Join(' ', rows.Select(a => a[0].ToString())) + "<br /><br />");
    Response.Write("B >>> " + string.Join(' ', rows.Select(b => b[1].ToString())) + "<br /><br />");
    Response.Write("C >>> " + string.Join(' ', rows.Select(c => c[2].ToString())) + "<br /><br />");
}

Edit #04

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSet ds = new DataSet("Test");

            DataTable table = ds.Tables.Add("TestTable");
            table.Columns.Add("A");
            table.Columns.Add("B");
            table.Columns.Add("C");
            DataRow dr = table.NewRow();
            dr["A"] = "0,59";
            dr["B"] = "1,54";
            dr["C"] = "1";
            table.Rows.Add(dr);
            DataRow dr2 = table.NewRow();
            dr2["A"] = "6,77";
            dr2["B"] = "0,13";
            dr2["C"] = "1,3";
            table.Rows.Add(dr2);

            DataTable dt = null;

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

                var rows = table.Rows.OfType<DataRow>().ToArray();

                string A = ("A >>> " + string.Join(' ', rows.Select(a => a[0].ToString())) + "<br /><br />");
                string B = ("B >>> " + string.Join(' ', rows.Select(b => b[1].ToString())) + "<br /><br />");
                string C = ("C >>> " + string.Join(' ', rows.Select(c => c[2].ToString())) + "<br /><br />");

                Console.WriteLine(A);
                Console.WriteLine(B);
                Console.WriteLine(C);
            }

            Console.ReadKey();
        }
    }
}

These are lines of error :

enter image description here

Error:

The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Read [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad), where you will see a lot of reasons why the answer to this question is **Absolutely yes!** – Zohar Peled Mar 22 '20 at 07:47
  • Is the data a string or a number? It looks like you have string data where the culture set the decimal point as a comma (not a period). So the numbers are 0, 16.11, and 3.08 Your query should be dt.AsEnumerable().Where(x => x.Field("A") < 0,1).Count(); – jdweng Mar 22 '20 at 07:52
  • @Golia Are you able to elaborate on "I can't print the value"? What's `Response.Write` – Hayden Mar 22 '20 at 10:51
  • @Hayden Thank you for reply, please see **Edit #02** in my first question –  Mar 22 '20 at 11:20
  • @Golia See my edit to my answer. – Hayden Mar 22 '20 at 12:00
  • @Hayden Thank you for reply, please see **Edit #03** in my first question. Now I have error... –  Mar 22 '20 at 13:31
  • @Golia See my new edit to my answer – Hayden Mar 22 '20 at 22:07

3 Answers3

0

Try adding a .ToString() before the .Split(). You'll probably also need a cast to double.

(double)dt.Rows[0]["A"].ToString().Split(',')
Slugsie
  • 851
  • 1
  • 7
  • 17
  • thanks but now the error is Cannot implicitly convert type 'string' to 'double' Issue –  Mar 22 '20 at 07:44
  • I edited my answer to add that you'll need to add a cast to double as well. – Slugsie Mar 22 '20 at 07:45
  • You can't cast an array of strings to a double. You can use linq's [`Cast`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.cast?view=netframework-4.8#System_Linq_Enumerable_Cast__1_System_Collections_IEnumerable_) to get an `IEnumerable` and then `ToArray()` to convert it to an array. – Zohar Peled Mar 22 '20 at 07:49
0

You need to change your code to cast to string, then parse the string to double, then cast to array.

using System.Linq;
...    

Double[] A = dt.Rows[0]["A"].ToString().Split(',').Select(double.Parse).ToArray();
Double[] B = dt.Rows[0]["B"].ToString().Split(',').Select(double.Parse).ToArray();
Double[] C = dt.Rows[0]["C"].ToString().Split(',').Select(double.Parse).ToArray();

Edit

After seeing your edits, to get the output you desire, all you need to do is:

if (ds.Tables.Count > 0)
{
    dt = ds.Tables[0];

    var rows = dt.Rows.OfType<DataRow>().ToArray();

    Response.Write("A >>> " + string.Join(' ', rows.Select(a => a[0].ToString())) + "<br /><br />");
    Response.Write("B >>> " + string.Join(' ', rows.Select(b => b[1].ToString())) + "<br /><br />");
    Response.Write("C >>> " + string.Join(' ', rows.Select(c => c[2].ToString())) + "<br /><br />");
}

Which I've tested, and gives the desired output (unless the Response.Write method does something else).

Also, in your loop in the edit, you're only ever getting the first row:

foreach (DataRow row in table.Rows)
{
    Double[] A = dt.Rows[0]["A"].ToString().Split(',').Select(double.Parse).ToArray();
    Double[] B = dt.Rows[0]["B"].ToString().Split(',').Select(double.Parse).ToArray();
    Double[] C = dt.Rows[0]["C"].ToString().Split(',').Select(double.Parse).ToArray();
    ...
}

Which should of been:

foreach (DataRow row in table.Rows)
{
    Double[] A = row["A"].ToString().Split(',').Select(double.Parse).ToArray();
    Double[] B = row["B"].ToString().Split(',').Select(double.Parse).ToArray();
    Double[] C = row["C"].ToString().Split(',').Select(double.Parse).ToArray();
    ...
}

Edit 2 Fixed copy paste fail.

Edit 3

Here's my testing code that I used:

using System;
using System.Data;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSet ds = new DataSet("Test");

            DataTable table = ds.Tables.Add("TestTable");
            table.Columns.Add("A");
            table.Columns.Add("B");
            table.Columns.Add("C");
            DataRow dr = table.NewRow();
            dr["A"] = "0,59";
            dr["B"] = "1,54";
            dr["C"] = "1";
            table.Rows.Add(dr);
            DataRow dr2 = table.NewRow();
            dr2["A"] = "6,77";
            dr2["B"] = "0,13";
            dr2["C"] = "1,3";
            table.Rows.Add(dr2);

            DataTable dt = null;

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

                var rows = table.Rows.OfType<DataRow>().ToArray();

                string A = ("A >>> " + string.Join(' ', rows.Select(a => a[0].ToString())) + "<br /><br />");
                string B = ("B >>> " + string.Join(' ', rows.Select(b => b[1].ToString())) + "<br /><br />");
                string C = ("C >>> " + string.Join(' ', rows.Select(c => c[2].ToString())) + "<br /><br />");

                Console.WriteLine(A);
                Console.WriteLine(B);
                Console.WriteLine(C);
            }

            Console.ReadKey();
        }
    }
}

Which for me gives the desired output. I think the error you're seeing maybe in a different spot in your application.

Hayden
  • 2,902
  • 2
  • 15
  • 29
  • Thank you for reply, please see **Edit #01** in my first question... –  Mar 22 '20 at 09:33
  • Thank you for reply, please see **Edit #04** in my first question.. –  Mar 23 '20 at 08:50
0

Invalid input parameter type.

Please, in your string replace ' ' with " "

string A = ("A >>> " + string.Join(" ", rows.Select(a => a[0].ToString())) + "<br /><br />");
string B = ("B >>> " + string.Join(" ", rows.Select(b => b[1].ToString())) + "<br /><br />");
string C = ("C >>> " + string.Join(" ", rows.Select(c => c[2].ToString())) + "<br /><br />");
Hamamelis
  • 1,983
  • 8
  • 27
  • 41