Linq 不是数字 c#
本文关键字:数字 Linq | 更新日期: 2023-09-27 18:36:16
Model.GetData.Where(p => p.Value != null).ToList();
我的数据库中有如下所示的值,
我的价值列:
Null
Null
5
16
ExampleValue1
ExampleValue2
p.值为字符串
我只想看到"ExampleValue1"和"ExampleValue2"。如何获取不为空且不为数字的数据?
也许:
var BillingNumbers = Model.GetData
.Where(p => p.Value != null && SqlFunctions.IsNumeric(p.Value) == 0);
假设结果的类型不同,您可以按类型进行查询,如下所示: var results = from item in collection where (item is String) select item;
示例程序:
static void Main(string[] args)
{
var collection = new List<Object>
{
5,
1.5,
null,
"foo",
"bar",
};
var results = from item in collection where (item is String) select item;
foreach (var result in results)
{
Console.WriteLine(result);
}
System.Threading.Thread.Sleep(10000);
}
你可以依靠这个代码:结果只包含字符串
List<object> result = new List<object>();
var MyList = new List<Object>{
523,
2.555,
null,
"ExampleValue1",
"ExampleValue2",
};
MyList.ForEach(x =>
{
if (x != null)
{
if (!string.IsNullOrEmpty(Regex.Replace(x.ToString(), @"^[0-9]*(?:'.[0-9]*)?$", string.Empty)))
{
result.Add(x);
}
}
});
试试这个。
int i;
var resultList = Model.GetData.Where(p => p.Value != null).ToList();
var finalResult= resultList.Where( t => !int.TryParse( t.Value , out i));