LINQ和SQL中的字符 [英] Characters in LINQ and SQL
本文介绍了LINQ和SQL中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试试这个sp和linq查询
SP
从tblRe中选择不同的区域其中Region
此查询返回
I try this sp and linq query
SP
select distinct Region from tblRe where Region
this query return
ISL
Paris
NY
SDF-23
LON
SDF-43
234
AM
RU
KAS
FJS-35
SDF-32
SDF-45
i不想要有数字/字符的价值..
我是怎么做的sql
然后我在linq中尝试这个
我尝试了什么:
i DONT want value which have numbers/characters..
how i do that in sql
and then i try this in linq
What I have tried:
var list = tea.tblRe.AsEnumerable()
.Where(x => !x.Region.Any(char.IsDigit))
.ToList().Distinct();
这只显示名字,但重复名称。
this show only names but with repetition ie.g
ISL
Paris
NY
NY
LON
KAS
AM
RU
KAS
LON
LON
AM
KAS
KAS
现在我不想重复
任何解决方案?
now i don't want repetition
any solu?
推荐答案
删除ToList - 它没有任何帮助 - 并检查您的数据。
当我在这里模拟你的代码时:
Remove the ToList - it does nothing to help - and check your data.
When I simulate your code here:
List<string> list = new List<string>();
list.Add("ISL");
list.Add("Paris");
list.Add("NY");
list.Add("SDF-23");
list.Add("LON");
list.Add("SDF-43");
list.Add("234");
list.Add("AM");
list.Add("RU");
list.Add("KAS");
list.Add("FJS-35");
list.Add("SDF-32");
list.Add("SDF-45");
var list2 = list.AsEnumerable()
.Where(x => !x.Any(char.IsDigit))
.Distinct();</string></string>
我的确得到了我的意思期待:
I get exactly what I expect:
ISL
Paris
NY
LON
AM
RU
KAS
没有任何重复。
问题是你运行的不是区域属性而是整个记录,这是一个复杂的对象。试试这个:
The problem is that you are running distinct not on the region property but on the whole record which is a complex object. Try this:
var list = tea.tblRe.AsEnumerable()
.Select(x => x.Region)
.Where(region => !region.Any(char.IsDigit))
.Distinct()
.ToList();
要在SQL中执行此操作,您需要使用NOT LIKE
比较:
To do this in SQL, you need to use aNOT LIKE
comparison:
SELECT DISTINCT
Region
FROM
tblRe
WHERE
Region Not Like '%[0-9]%'
;
LIKE(Transact- SQL) [ ^ ]
这篇关于LINQ和SQL中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文