本文整理汇总了C#中Section.Select方法的典型用法代码示例。如果您正苦于以下问题:C# Section.Select方法的具体用法?C# Section.Select怎么用?C# Section.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Section
的用法示例。
在下文中一共展示了Section.Select方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetInstruments
/// <summary>
/// Gets a list of Instrument objects filtered by a section
/// </summary>
/// <param name="selectedSection">A section to filter instruments by</param>
/// <returns></returns>
//public List<Instrument> GetInstruments(Section selectedSection)
//{
// using (dbconn = new SqlConnection(Music.Properties.Settings.Default.orchestraConnection))
// {
// dbconn.Open();
// dbcomm = new SqlCommand(String.Format("select Instrument, Type from Instruments where Type = '{0}' order by Type asc",selectedSection.ToString()), dbconn);
// dbreader = dbcomm.ExecuteReader();
// List<Instrument> instruments = new List<Instrument>();
// while (dbreader.Read())
// {
// string instrumentName = dbreader["Instrument"].ToString();
// Section instrumentType;
// if (Enum.TryParse<Section>(dbreader["Type"].ToString(), true, out instrumentType))
// instruments.Add(new Instrument(instrumentName, instrumentType));
// else
// instruments.Add(new Instrument());
// }
// dbconn.Close();
// return instruments;
// }
//}
/// <summary>
/// Gets a list of Instrument objects filtered by an array of Sections
/// </summary>
/// <param name="selectedSection">An array to filter instruments by</param>
/// <returns></returns>
public List<Instrument> GetInstruments(Section[] selectedSection)
{
try
{
using (dbconn = new SqlConnection(Music.Properties.Settings.Default.orchestraConnection))
{
dbconn.Open();
//This command creates a sql string that checks for instruments of a type contained in the parameter list which is created by using a string join and some string formats
dbcomm = new SqlCommand(String.Format("select Instrument, Type from Instruments where Type in({0}) order by Instrument asc", String.Join(",", selectedSection.Select(x => String.Format("'{0}'", x.ToString())).ToArray())), dbconn);
dbreader = dbcomm.ExecuteReader();
List<Instrument> instruments = new List<Instrument>();
while (dbreader.Read())
{
string instrumentName = dbreader["Instrument"].ToString();
Section instrumentType;
//This sneaky little section tries to parse the instrument value of the instrument by using the Enum.TryParse which returns a bool, if false there is a bad type and a default instrument replaces it
if (Enum.TryParse<Section>(dbreader["Type"].ToString(), true, out instrumentType))
instruments.Add(new Instrument(instrumentName, instrumentType));
else
instruments.Add(new Instrument());
}
return instruments;
}
}
catch
{
throw new ArgumentException("Something has gone horribly, horribly wrong. Most likely my SQL instance is unreachable or down.");
}
}