如何获得独特的价值 [英] How to get distinct value
本文介绍了如何获得独特的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
Example:
Kumar
Kumar1
Kumar2
123Kumar
2Kumar1
Muruga1
Muruga2
123Muruga
1Muruga3
结果:
result:
Kumar,Muruga
如何解决。不要使用foreach条件?请帮助这个答案。
我尝试过:
[删除了问题正文中已提供的重复信息。没有提供SQL命令或源代码。]
How to solve. Don't used foreach condition? pls help this answer.
What I have tried:
[Removed duplicate information already provided in the question body. No SQL command or source code provided.]
推荐答案
相当多,你不能:那里没有明确的值 - 或者说它们都是不同的值。 br />
尝试查找包含其他行的行在SQL中很糟糕 - 字符串处理最多是原始的,你需要删除所有非字母字符。
Do在演示语言中,它非常简单(如果仍然看起来很讨厌):
Pretty much, you can't: there are no distinct values there - or rather they are all distinct values.
And trying to look for rows which contain other rows is horrible in SQL - the string handling is primitive at best, and you need to remove all non-alpha characters.
Do it in the presentation language, and it's pretty simple (if still nasty looking):
string[] names = { "Kumar", "Kumar1", "Kumar2", "123Kumar", "2Kumar1", "Muruga1", "Muruga2", "123Muruga", "1Muruga3" };
string[] distinct = names.Select(n => new string((n.Where(c => char.IsLetter(c))).ToArray())).Distinct().ToArray();
CREATE TABLE yourtable
([ID] int, [User] varchar(10), [Department] varchar(8))
;
INSERT INTO yourtable
([ID], [User], [Department])
VALUES
(1, 'User1', 'Admin'),
(2, 'User1', 'Accounts'),
(3, '1User2', 'Finance'),
(4, 'User3', 'Sales'),
(5, '123User3', 'Finance')
;
select distinct REPLACE(REPLACE(REPLACE(t.[user], '1', ''), '2', ''), '3', '')
from yourtable t;
也许这个:
Maybe this:
internal static class Program
{
private static void Main()
{
var list = new List<string>() {
"Kumar",
"Kumar1",
"Kumar2",
"123Kumar",
"2Kumar1",
"Muruga1",
"Muruga2",
"123Muruga",
"1Muruga3"
};
foreach (var result in list.Select(x => GetLongestMatch(x, list.Except(new List<string> { x }))).Distinct())
Console.WriteLine(
这篇关于如何获得独特的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文