C# 查找表匹配的最佳方法
我需要一种基于多个关键字在对象列表中查找匹配行的方法。在下面的代码示例中,我需要实现FindID()函数 这些规则是:C# 查找表匹配的最佳方法,c#,match,keyword,C#,Match,Keyword,我需要一种基于多个关键字在对象列表中查找匹配行的方法。在下面的代码示例中,我需要实现FindID()函数 这些规则是: 表中的某些行将*作为值(通配符),这意味着它们将匹配任何值 匹配的优先级是从左到右(即,更重要的是匹配左侧的值) 一些例子: FindID(“约翰”、“黑色”、“棕色”)-应返回1,完全匹配 FindID(“约翰”、“红色”、“绿色”)-应返回5(不是7),匹配“John”和其他两个通配符(*) FindID(“约翰”、“红色”、“棕色”)-应返回6,匹配“John”和“Bro
FindID(“约翰”、“黑色”、“棕色”)代码>-应返回1,完全匹配
FindID(“约翰”、“红色”、“绿色”)代码>-应返回5(不是7),匹配“John”和其他两个通配符(*)
FindID(“约翰”、“红色”、“棕色”)代码>-应返回6,匹配“John”和“Brown”以及一个通配符
FindID(“布莱恩”、“格雷”、“格雷”)代码>-应返回8,匹配三个通配符
下面的代码是完整的,可以执行
有人知道最好的方法吗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TableScan
{
class Program
{
public static TableClass _MyTable = new TableClass();
public class TableRow
{
public int ID;
public string Name;
public string Hair;
public string Eyes;
public TableRow(int _ID, string _Name, string _Hair, string _Eyes)
{
ID = _ID;
Name = _Name;
Hair = _Hair;
Eyes = _Eyes;
}
}
public class TableClass
{
public List<TableRow> Table = new List<TableRow>();
}
static void Main(string[] args)
{
// ID Name Hair Eyes
// 1 John Black Brown
// 2 Paul Brown Green
// 3 Ringo Blond Blue
// 4 George Red Blue
// 5 John * *
// 6 John * Brown
// 7 * Red *
// 8 * * *
// 9 Paul * *
CreateTable();
ShowTable();
FindID("John", "Black", "Brown"); // should return 1, complete match
FindID("John", "Red", "Green"); // should return 5 (not 7), matched on "John" and the other two wildcards (*)
FindID("John", "Red", "Brown"); // should return 6, matched on "John" and "Brown" and one wildcard
FindID("Brian", "Grey", "Grey"); // should return 8, matched on three wildcard
while (Console.ReadKey().Key == 0) { }
}
static int FindID(string _Name, string _Hair, string _Eyes)
{
// needs to be implemented
return 0;
}
static void CreateTable()
{
_MyTable.Table.Add(new TableRow(1, "John", "Black", "Brown"));
_MyTable.Table.Add(new TableRow(2, "Paul", "Brown", "Green"));
_MyTable.Table.Add(new TableRow(3, "Ringo", "Blond", "Blue"));
_MyTable.Table.Add(new TableRow(4, "George", "Red", "Blue"));
_MyTable.Table.Add(new TableRow(5, "John", "*", "*"));
_MyTable.Table.Add(new TableRow(6, "John", "*", "Brown"));
_MyTable.Table.Add(new TableRow(7, "*", "Red", "*"));
_MyTable.Table.Add(new TableRow(8, "*", "*", "*"));
_MyTable.Table.Add(new TableRow(9, "Paul", "*", "*"));
}
static void ShowTable()
{
foreach(TableRow _TableRow in _MyTable.Table)
{
Console.WriteLine("{0} {1} {2} {3}", _TableRow.ID, _TableRow.Name, _TableRow.Hair, _TableRow.Eyes);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间表扫描
{
班级计划
{
公共静态TableClass_MyTable=新TableClass();
公共类表格行
{
公共int ID;
公共字符串名称;
公共线毛;
公众的眼睛;
公共表行(int\u ID、string\u Name、string\u Hair、string\u Eyes)
{
ID=_ID;
名称=_名称;
头发=_头发;
眼睛=_眼睛;
}
}
公共课
{
公共列表表=新列表();
}
静态void Main(字符串[]参数)
{
//ID名称头发眼睛
//约翰·布莱克·布朗
//保罗·布朗·格林
//3林戈金黄色蓝色
//乔治红蓝
//5约翰**
//6约翰*布朗
//7*红色*
// 8 * * *
//9保罗**
CreateTable();
可显示();
FindID(“John”、“Black”、“Brown”);//应返回1,完成匹配
FindID(“John”、“Red”、“Green”);//应该返回5(不是7),匹配“John”和其他两个通配符(*)
FindID(“John”、“Red”、“Brown”);//应该返回6,匹配“John”和“Brown”以及一个通配符
FindID(“Brian”、“Grey”、“Grey”);//应该返回8,匹配三个通配符
而(Console.ReadKey().Key==0){}
}
静态int FindID(字符串名称、字符串头发、字符串眼睛)
{
//需要加以实施
返回0;
}
静态void CreateTable()
{
_MyTable.Table.Add(新表格行(1,“John”、“Black”、“Brown”);
_MyTable.Table.Add(新表格行(2,“Paul”、“Brown”、“Green”);
_MyTable.Table.Add(新的TableRow(3,“Ringo”、“金发”、“蓝色”);
_MyTable.Table.Add(新TableRow(4,“乔治”、“红色”、“蓝色”);
_MyTable.Table.Add(新表行(5,“John”、“*”、“*”));
_MyTable.Table.Add(新TableRow(6,“John”,“*”,“Brown”);
_MyTable.Table.Add(新TableRow(7,“*”、“红色”、“*”));
_MyTable.Table.Add(新的TableRow(8,“*”,“*”,“*”);
_MyTable.Table.Add(新TableRow(9,“Paul”、“*”、“*”));
}
静态void ShowTable()
{
foreach(TableRow\u MyTable.Table中的TableRow)
{
Console.WriteLine(“{0}{1}{2}{3}”、_TableRow.ID、_TableRow.Name、_TableRow.Hair、_TableRow.Eyes);
}
}
}
}
好的,今晚就死定了,这里有一个基于注释的解决方案,请参见
我觉得我们是在为你做作业……我同意这个简单化的例子看起来像是作业,但实际上我写它是为了简化我正在做的一个个人项目的问题(事实上我比学龄年龄大很多:)。谢谢你看一看。我没有时间精确地给出答案,但我会创建一个静态方法来指示一行是否匹配,然后创建一个方法,给一个匹配一个分数,其中精确匹配的分数高于通配符,并且从左到右减少匹配。然后只需执行(从myTable中的行开始,其中Equals(row,valueToMatch)按分数排序(row)选择row)。我会试试看。
static bool match(TableRow row, string _Name, string _Hair, string _Eyes)
{
return (row.Name == _Name || row.Name == "*") && (row.Hair == _Hair || row.Hair == "*") && (row.Eyes == _Eyes || row.Eyes == "*");
}
static int score(TableRow row)
{
//can assume these already "match"
var score = 0;
score += row.Name == "*" ? 1000 : 2000;
score += row.Hair == "*" ? 100 : 200;
score += row.Eyes == "*" ? 10 : 20;
return score;
}
static int FindID(string _Name, string _Hair, string _Eyes)
{
var s = from row in _MyTable.Table
where match(row, _Name, _Hair, _Eyes)
orderby score(row) descending
select row;
var match = s.FirstOrDefault();
if(match != null) return match.ID;
return -1; //i guess?
}