Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need LINQ query value between two numbers.

But, my case is two number is different.

the comparison value data type is double.

finding the value in the column datatype is string(text)

i tried to convert ToDouble. But, it is not working.

Query below

MIN and MAX Value datatype is double Ex : 200 and 210

MY ABC column datatype is string(text) and value like "205","198","215"


C#
var Qry = (from r in tbl1.AsEnumerable()

where Convert.ToDouble(r.Field<string>("ABC")) >= Convert.ToDouble(Min_VALUE)
&& Convert.ToDouble(r.Field<string>("ABC")) >= Convert.ToDouble(Max_VALUE)
select new
{
A_VALUE = Convert.ToDouble(r.Field<string>("ABC")),
}).Count();


I want in between values from 200 and 210.

so the result is "205"

How to write the linq query for string and number..?

What I have tried:

MIN and MAX Value datatype is double Ex : 200 and 210

MY ABC column datatype is string(text) and value like "205","198","215"


C#
var Qry = (from r in tbl1.AsEnumerable()

                       where Convert.ToDouble(r.Field<string>("ABC")) >= Convert.ToDouble(Min_VALUE)
                             && Convert.ToDouble(r.Field<string>("ABC")) >= Convert.ToDouble(Max_VALUE)
                         select new
                         {
                             A_VALUE = Convert.ToDouble(r.Field<string>("ABC")),
                         }).Count();
Posted
Updated 27-May-17 23:41pm
v2

You're comparing where ABC >= min and ABC >= max, so it will only be true for values >= max. I think you mean ABC >= min and ABC <= max

where Convert.ToDouble(r.Field<string>("ABC")) >= Convert.ToDouble(Min_VALUE)
                             && Convert.ToDouble(r.Field<string>("ABC")) <= Convert.ToDouble(Max_VALUE)
 
Share this answer
 
v3
If i understand you well, you want to get in between value from the string-list of numbers. Check this:
C#
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ABC", typeof(string)));
dt.Rows.Add(new object[]{"205,198,215"});
dt.Rows.Add(new object[]{"105,198,315"});
dt.Rows.Add(new object[]{"405,298,215"});
dt.Rows.Add(new object[]{"305,398,315"});
dt.Rows.Add(new object[]{"305,298,415"});


var result = dt.AsEnumerable()
	.Select(x => new 
		{
			Text = x.Field<string>("ABC"),
			Numbers = x.Field<string>("ABC")
				.Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries)
				.Select(y => Convert.ToInt32(y))
				.ToList()
		})
	.Select(x => new
		{
			Text = x.Text,
			MinVal = x.Numbers.Min(),
			MaxVal = x.Numbers.Max(),
			InBetweenVal = x.Numbers.First(y => y>x.Numbers.Min() && y<x.Numbers.Max())
		})
	.ToList();

Console.WriteLine("Text\tMinVal\tMaxVal\tInBetweenVal");
foreach(var r in result)
{
	Console.WriteLine("{0}\t{1}\t{2}\t{3}", r.Text, r.MinVal, r.MaxVal, r.InBetweenVal);
}


Result:
Text        MinVal  MaxVal  InBetweenVal
205,198,215 198     215     205
105,198,315 105     315     198
405,298,215 215     405     298
305,398,315 305     398     315
305,298,415 298     415     305
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900