使用LINQ查询获取两个数字之间的值 [英] Get value between two numbers using LINQ query

查看:78
本文介绍了使用LINQ查询获取两个数字之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要两个数字之间的LINQ查询值。



但是,我的情况两个数字是不同的。



比较值数据类型是双倍。



找到值列数据类型是字符串(文本)



i尝试转换为ToDouble。但是,它不起作用。



查询如下



MIN和MAX值数据类型是双倍Ex: 200和210



MY ABC列数据类型是字符串(文本),值类似于205,198,215





  var  Qry =(来自 r   tbl1.AsEnumerable()

其中​​ Convert.ToDouble(r.Field< string>( ABC))> = Convert.ToDouble(Min_VALUE)
&& Convert.ToDouble(r.Field< string>( ABC))> = Convert.ToDouble(Max_VALUE)
选择 new
{
A_VALUE = Convert.ToDouble(r.Field< stri ng>( ABC)),
})。Count();





我想要介于200和210之间的值。



所以结果是205



如何写字符串和数字的linq查询..?



< b>我尝试了什么:



MIN和MAX Value数据类型是双倍Ex:200和210


MY ABC列数据类型为字符串(文本),值类似于205,198,215





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

where Convert.ToDouble(r.Field< string>( ABC))> =转换。 ToDouble(Min_VALUE)
&安培;&安培; Convert.ToDouble(r.Field< string>( ABC))> = Convert.ToDouble(Max_VALUE)
选择 new
{
A_VALUE = Convert.ToDouble(r.Field< string>( ABC )),
})。Count();

解决方案

你在比较ABC> = min和ABC> = max,因此对于值> gt = = max只会为true。我认为你的意思是ABC> = min和ABC < = max



其中Convert.ToDouble(r .Field< string>(ABC))> = Convert.ToDouble(Min_VALUE)
&& Convert.ToDouble(r.Field< string>(ABC))< = Convert.ToDouble(Max_VALUE)


如果我理解你,那么你想要从字符串列表中获取值。检查一下:

 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)
。选择(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\mMinVal\tMaxVal\tInBetweenVal);
foreach var r in result)
{
Console.WriteLine( {0} \t {1} \t {2} \t {3},r.Text,r.MinVal,r.MaxVal,r.InBetweenVal);
}





结果:

文本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


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"


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"


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();

解决方案

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)


If i understand you well, you want to get in between value from the string-list of numbers. Check this:

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


这篇关于使用LINQ查询获取两个数字之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆