本文整理汇总了C#中MultiDictionary.Where方法的典型用法代码示例。如果您正苦于以下问题:C# MultiDictionary.Where方法的具体用法?C# MultiDictionary.Where怎么用?C# MultiDictionary.Where使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MultiDictionary
的用法示例。
在下文中一共展示了MultiDictionary.Where方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessCommands
private static string ProcessCommands(MultiDictionary<string, MultiDictionary<string, string>> phoneBook)
{
StreamReader commandsReader = new StreamReader("commands.txt");
StringBuilder result = new StringBuilder();
using (commandsReader)
{
string command = commandsReader.ReadLine();
while (command != null)
{
int indexOfOpenBracket = command.IndexOf('(');
string withoutCommandName = command.Substring(indexOfOpenBracket + 1).TrimEnd(')');
var splitted = withoutCommandName.Split(',');
string nameFromCommand = splitted[0].Trim();
// if the command is search by name only
if (splitted.Length == 1)
{
// get all elements which have got the same name as the searched name from command
var resultOnlyByName = phoneBook.Where(p => p.Key == nameFromCommand);
foreach (var nameEntries in resultOnlyByName)
{
foreach (var townPhoneCollection in nameEntries.Value)
{
foreach (var phoneCollection in townPhoneCollection)
{
foreach (var phoneNumber in phoneCollection.Value)
{
string addingLineToResult = string.Format("{0} from {1} -> {2}", nameEntries.Key, phoneCollection.Key, phoneNumber);
result.AppendLine(addingLineToResult);
}
}
}
}
// added to divide different commands results
result.AppendLine("------------------------------------------------");
}
else if (splitted.Length == 2)
{
// if the command is search by name and location
string town = splitted[1].Trim();
// get all elements which have got the same name as the searched name from command
var currentResultByName = phoneBook.Where(p => p.Key == nameFromCommand);
foreach (var nameEntries in currentResultByName)
{
foreach (var townPhoneCollection in nameEntries.Value)
{
// then get all elements which have got the same town as the searched town from command
var resultAndByTown = townPhoneCollection.Where(t => t.Key == town);
foreach (var phoneCollection in resultAndByTown)
{
foreach (var phoneNumber in phoneCollection.Value)
{
string addingLineToResult = string.Format("{0} from {1} -> {2}", nameEntries.Key, phoneCollection.Key, phoneNumber);
result.AppendLine(addingLineToResult);
}
}
}
}
// added to divide different commands results
result.AppendLine("------------------------------------------------");
}
command = commandsReader.ReadLine();
}
}
return result.ToString();
}
示例2: Main
public static void Main(string[] args)
{
StreamReader phones = new StreamReader("..\\..\\phones.txt");
var phonebook = new MultiDictionary<string, MultiDictionary<string, string>>(false);
using (phones)
{
while (!phones.EndOfStream)
{
string entry = phones.ReadLine();
string[] entryInfo = entry.Split('|');
string name = entryInfo[0].Trim();
string location = entryInfo[1].Trim();
string phoneNumber = entryInfo[2].Trim();
if (phonebook.ContainsKey(name))
{
var nameInPhoneBook = phonebook[name].First();
if (nameInPhoneBook.ContainsKey(location))
{
nameInPhoneBook[location].Add(phoneNumber);
}
else
{
nameInPhoneBook.Add(location, phoneNumber);
}
}
else
{
phonebook.Add(name, new MultiDictionary<string, string>(true));
var nameInPhoneBook = phonebook[name].First();
nameInPhoneBook.Add(location, phoneNumber);
}
}
}
var commands = new StreamReader("..\\..\\commands.txt");
using (commands)
{
while (!commands.EndOfStream)
{
string commandString = commands.ReadLine();
string[] parameters = commandString
.Substring(commandString.IndexOf('(') + 1, commandString.IndexOf(')') - commandString.IndexOf('(') - 1)
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string name = parameters[0].Trim();
string location = string.Empty;
if (parameters.Length == 2)
{
location = parameters[1].Trim();
}
Console.WriteLine(commandString);
if (location == string.Empty)
{
var resultsByName = phonebook.Where(x => x.Key == name);
foreach (var nameEntries in resultsByName)
{
foreach (var locationPhoneCollection in nameEntries.Value)
{
foreach (var phonesCollection in locationPhoneCollection)
{
foreach (var phoneNumber in phonesCollection.Value)
{
Console.WriteLine("{0} -> {1} -> {2}",
nameEntries.Key, phonesCollection.Key, phoneNumber);
}
}
}
}
}
else
{
var resultsByName = phonebook.Where(x => x.Key == name);
foreach (var nameEntries in resultsByName)
{
foreach (var locationPhoneCollection in nameEntries.Value)
{
var nameAndLocation = locationPhoneCollection.Where(x => x.Key == location);
foreach (var phonesCollection in nameAndLocation)
{
foreach (var phoneNumber in phonesCollection.Value)
{
Console.WriteLine("{0} -> {1} -> {2}", nameEntries.Key, phonesCollection.Key, phoneNumber);
}
}
}
}
}
}
}
}