public MutableString RemoveAllExcept(IEnumerable <char> values) { if (null == values) { throw new ArgumentNullException("values"); } if (0 == _value.Length) { return(this); } #if NET20 var list = IEnumerableExtensionMethods.ToList(values); #else var list = values.ToList(); #endif if (0 == list.Count) { return(Clear()); } for (var i = _value.Length - 1; i > -1; i--) { if (list.Contains(_value[i])) { continue; } Remove(i, 1); } return(this); }
public static HttpHeaderDictionary FromString(string value) { if (null == value) { throw new ArgumentNullException("value"); } var result = new HttpHeaderDictionary(); #if NET20 var lines = IEnumerableExtensionMethods.ToQueue(StringExtensionMethods.Split(value, Environment.NewLine, StringSplitOptions.None)); #else var lines = value.Split(Environment.NewLine, StringSplitOptions.None).ToQueue(); #endif if (0 == lines.Count) { return(result); } while (0 != lines.Count) { var line = lines.Dequeue(); if (0 == line.Length) { break; } result.Add(HttpHeader.FromString(line)); } return(result); }
private static bool HasVerticalColumns(HtmlNode table) { #if NET20 if (0 != IEnumerableExtensionMethods.Count(table.Descendants("thead"))) #else if (table.Descendants("thead").Any()) #endif { return(false); } #if NET20 var row = IEnumerableExtensionMethods.FirstOrDefault(table.Descendants("tr")); #else var row = table.Descendants("tr").FirstOrDefault(); #endif if (null == row) { return(false); } #if NET20 return(0 != IEnumerableExtensionMethods.Count(row.Descendants("th"))); #else return(row.Descendants("th").Any()); #endif }
public IEnumerable <Element> GetChildren(EntityToken entityToken, SearchToken searchToken) { if (UserValidationFacade.IsLoggedIn() && !DataLocalizationFacade.ActiveLocalizationCultures.Contains(UserSettings.ActiveLocaleCultureInfo)) { return(Enumerable.Empty <Element>()); } if (entityToken is AssociatedDataElementProviderHelperEntityToken associatedData) { return(_pageAssociatedHelper.GetChildren(associatedData, false)); } if (entityToken is DataGroupingProviderHelperEntityToken dataGrouping) { return(_pageAssociatedHelper.GetChildren(dataGrouping, false)); } using (new DataScope(DataScopeIdentifier.Administrated)) { var allChildPages = GetChildrenPages(entityToken, searchToken); List <KeyValuePair <PageLocaleState, IPage> > childPages = IEnumerableExtensionMethods.ToList(allChildPages, f => new KeyValuePair <PageLocaleState, IPage>(PageLocaleState.Own, f)); List <Element> childPageElements = GetElements(childPages, entityToken is PageElementProviderEntityToken); return(GetChildElements(entityToken, childPageElements)); } }
private static DataRow FillNormalDataRow(DataRow obj, HtmlNode row) { #if NET20 var cells = IEnumerableExtensionMethods.ToList(row.Descendants("td")); #else var cells = row.Descendants("td").ToList(); #endif if (0 == cells.Count) { return(obj); } var i = 0; foreach (var cell in cells) { var attribute = cell.Attributes["colspan"]; var span = null == attribute ? 1 : XmlConvert.ToInt32(attribute.Value); for (var s = 0; s < span; s++) { obj[i++] = cell; } } return(obj); }
public virtual void Save(Lexicon lexicon) { Trace.WriteIf(Tracing.Is.TraceVerbose, string.Empty); if (null == lexicon) { throw new ArgumentNullException("lexicon"); } using (var writers = new StreamWriterDictionary("CANONICAL,SYNONYMS") { Access = FileAccess.Write, Mode = FileMode.Create, Share = FileShare.None }) { #if NET20 if (0 == IEnumerableExtensionMethods.Count(lexicon)) #else if (!lexicon.Any()) #endif { writers.Item(Location.FullName).WriteLine(string.Empty); return; } #if NET20 var items = new SortedList <string, LexicalItem>(); foreach (var item in lexicon) { items.Add(item.CanonicalForm, item); } foreach (var item in items) { var synonyms = new SortedList <string, string>(); foreach (var synonym in item.Value.Synonyms) { synonyms.Add(synonym, synonym); } writers .Item(Location.FullName) .WriteLine(StringExtensionMethods.FormatWith( "{0},{1}", CsvStringExtensionMethods.FormatCommaSeparatedValue(item.Value.CanonicalForm), CsvStringExtensionMethods.FormatCommaSeparatedValue(IEnumerableExtensionMethods.Concat(synonyms.Values, ';')))); } #else foreach (var item in lexicon.OrderBy(x => x.CanonicalForm)) { writers .Item(Location.FullName) .WriteLine("{0},{1}".FormatWith( item.CanonicalForm.FormatCommaSeparatedValue(), item.Synonyms.OrderBy(x => x).Concat(';').FormatCommaSeparatedValue())); } #endif } }
public override IEnumerable <object[]> GetData(MethodInfo methodUnderTest, Type[] parameterTypes) { if (null == methodUnderTest) { throw new ArgumentNullException("methodUnderTest"); } if (null == parameterTypes) { throw new ArgumentNullException("parameterTypes"); } #if NET20 if (IEnumerableExtensionMethods.Count(Locations) != parameterTypes.Length) { throw new InvalidOperationException(StringExtensionMethods.FormatWith(Resources.Attribute_CountsDiffer, IEnumerableExtensionMethods.Count(Locations), parameterTypes.Length)); } #else if (Locations.Count() != parameterTypes.Length) { throw new InvalidOperationException(Resources.Attribute_CountsDiffer.FormatWith(Locations.Count(), parameterTypes.Length)); } #endif var list = new List <object>(); var index = -1; foreach (var location in Locations) { index++; if (parameterTypes[index] == typeof(HtmlDocument) || parameterTypes[index] == typeof(IXPathNavigable)) { list.Add(Download(location)); continue; } if (parameterTypes[index] == typeof(XPathNavigator)) { list.Add(Download(location).CreateNavigator()); continue; } if (parameterTypes[index] == typeof(DataSet)) { #if NET20 list.Add(HtmlDocumentExtensionMethods.TabularData(Download(location))); #else list.Add(Download(location).TabularData()); #endif continue; } throw new InvalidOperationException(Resources.HtmlAttribute_UnsupportedParameterType); } yield return(list.ToArray()); }
private static void AddVerticalDataRows(DataTable obj, HtmlNode body) { #if NET20 var rows = IEnumerableExtensionMethods.ToList(body.Descendants("tr")); var cells = 0; foreach (var row in rows) { cells = Math.Max(cells, IEnumerableExtensionMethods.Count(row.Descendants("td"))); } #else var rows = body.Descendants("tr").ToList(); var cells = rows .Select(row => row.Descendants("td").Count()) .Concat(new[] { 0 }) .Max(); #endif for (var i = 0; i < cells; i++) { obj.Rows.Add(obj.NewRow()); } var column = 0; foreach (var item in rows) { var current = column; var row = -1; foreach (var cell in item.Descendants("td")) { row++; for (var c = current; c < obj.Columns.Count; c++) { if (DBNull.Value == obj.Rows[row][current]) { break; } row++; } var attribute = cell.Attributes["rowspan"]; var span = null == attribute ? 1 : XmlConvert.ToInt32(attribute.Value); if (1 == span) { obj.Rows[row][current] = cell; continue; } for (var s = 0; s < span; s++) { obj.Rows[row][current + s] = cell; } } column++; } }
public LexicalMatch MatchWithin(string value) { if (null == value) { throw new ArgumentNullException("value"); } if (0 == value.Length) { return(null); } var result = Match(value); if (null != result) { return(result); } var parts = value.Split(' '); if (parts.Length < 2) { return(null); } var mutable = new MutableString(value); #if NET20 foreach (var part in PartsWithin(IEnumerableExtensionMethods.ToList(mutable.ToString().Split(' ')))) #else foreach (var part in PartsWithin(mutable.ToString().Split(' ').ToList())) #endif { result = Match(part); if (null == result) { continue; } #if NET20 var splits = StringExtensionMethods.Split(value, part, StringSplitOptions.None); #else var splits = value.Split(part, StringSplitOptions.None); #endif if (2 != splits.Length) { continue; } result.Prefix = splits[0].Trim(); result.Suffix = splits[1].Trim(); return(result); } return(null); }
private static IList <string> Columns(HtmlNode table) { #if NET20 var head = IEnumerableExtensionMethods.FirstOrDefault(table.Descendants("thead")); #else var head = table.Descendants("thead").FirstOrDefault(); #endif return(null == head?ColumnsRow(table) : ColumnsHead(head)); }
private static void AddVerticalDataColumns(DataTable obj, HtmlNode body) { var i = -1; var span = 1; foreach (var row in body.Descendants("tr")) { i++; if (span > 1) { span--; continue; } #if NET20 var heading = IEnumerableExtensionMethods.FirstOrDefault(row.Descendants("th")); #else var heading = row.Descendants("th").FirstOrDefault(); #endif if (null == heading) { obj.Columns.Add(XmlConvert.ToString(i), typeof(HtmlNode)); continue; } var attribute = heading.Attributes["rowspan"]; span = null == attribute ? 1 : XmlConvert.ToInt32(attribute.Value); #if NET20 || NET35 var text = HttpUtility.HtmlDecode(heading.InnerText); #else var text = WebUtility.HtmlDecode(heading.InnerText); #endif if (1 == span) { obj.Columns.Add(text, typeof(HtmlNode)); continue; } for (var s = 0; s < span; s++) { #if NET20 obj.Columns.Add(StringExtensionMethods.FormatWith("{0} ({1})", text, s + 1), typeof(HtmlNode)); #else obj.Columns.Add("{0} ({1})".FormatWith(text, s + 1), typeof(HtmlNode)); #endif } } }
public LexicalMatch MatchBeginning(string value) { if (null == value) { throw new ArgumentNullException("value"); } if (0 == value.Length) { return(null); } var result = Match(value); if (null != result) { return(result); } var substring = new MutableString(value); var clone = substring.Clone(); #if NET20 foreach (var word in IEnumerableExtensionMethods.Reverse(substring.ToString().Split(' '))) #else foreach (var word in substring.ToString().Split(' ').Reverse()) #endif { if (0 == word.Length) { continue; } substring.RemoveFromEnd(word).Trim(); result = Match(substring); if (null == result) { continue; } result.Suffix = clone.Suffix(substring).Trim(); return(result); } return(null); }
public static HttpStatusLine FromString(string value) { if (null == value) { throw new ArgumentNullException("value"); } if (0 == value.Length) { throw new ArgumentOutOfRangeException("value"); } #if NET20 var parts = IEnumerableExtensionMethods.ToQueue(StringExtensionMethods.Split(value, ' ', StringSplitOptions.RemoveEmptyEntries)); #else var parts = value.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToQueue(); #endif var version = parts.Peek().StartsWith("HTTP/", StringComparison.Ordinal) ? (HttpVersion)parts.Dequeue() : HttpVersion.Latest; if (0 == parts.Count) { throw new FormatException(); } var code = XmlConvert.ToInt32(parts.Dequeue()); if (0 == parts.Count) { return(new HttpStatusLine(version, (HttpStatusCode)code)); } #if NET20 var reason = string.Empty; foreach (var part in parts) { reason += " " + part; } #else var reason = parts.Aggregate(string.Empty, (current, part) => current + (" " + part)); #endif return(new HttpStatusLine(version, code, reason.Trim())); }
protected virtual IList <string> NextLine() { Line = null; while (!EndOfStream) { Line = ReadLine(); LineNumber++; if (!string.IsNullOrEmpty(Line)) { break; } } return(string.IsNullOrEmpty(Line) ? null #if NET20 : IEnumerableExtensionMethods.ToList(Line.Split('\t')));
public IEnumerable <Element> GetChildren(EntityToken entityToken, SearchToken searchToken) { if (entityToken is AssociatedDataElementProviderHelperEntityToken) { return(_pageAssociatedHelper.GetChildren((AssociatedDataElementProviderHelperEntityToken)entityToken, false)); } if (entityToken is DataGroupingProviderHelperEntityToken) { return(_pageAssociatedHelper.GetChildren((DataGroupingProviderHelperEntityToken)entityToken, false)); } using (new DataScope(DataScopeIdentifier.Administrated)) { var allChildPages = GetChildrenPages(entityToken, searchToken); List <KeyValuePair <PageLocaleState, IPage> > childPages = IEnumerableExtensionMethods.ToList(allChildPages, f => new KeyValuePair <PageLocaleState, IPage>(PageLocaleState.Own, f)); List <Element> childPageElements = GetElements(childPages, entityToken is PageElementProviderEntityToken); return(GetChildElements(entityToken, childPageElements)); } }
private static IList <string> ColumnsRow(HtmlNode table) { var list = new List <string>(); #if NET20 var row = IEnumerableExtensionMethods.FirstOrDefault(table.Descendants("tr")); #else var row = table.Descendants("tr").FirstOrDefault(); #endif if (null == row) { return(list); } #if NET20 var headers = IEnumerableExtensionMethods.ToList(row.Descendants("th")); #else var headers = row.Descendants("th").ToList(); #endif if (0 == headers.Count) { #if NET20 headers = IEnumerableExtensionMethods.ToList(row.Descendants("td")); #else headers = row.Descendants("td").ToList(); #endif } #if NET20 foreach (var header in headers) { list.Add(ColumnName(header)); } #else list.AddRange(headers.Select(ColumnName)); #endif return(list); }
private static void AddDataTable(DataSet data, HtmlNode table) { var result = new DataTable { Locale = CultureInfo.InvariantCulture }; var id = table.Attributes["id"]; if (null != id) { var name = id.Value; if (null != data.Tables[name]) { return; } result.TableName = name; } else { #if NET20 var caption = IEnumerableExtensionMethods.FirstOrDefault(table.Descendants("caption")); #else var caption = table.Descendants("caption").FirstOrDefault(); #endif if (null != caption) { result.TableName = caption.InnerText; } } FillDataTable(result, table); data.Tables.Add(result); }
private static void FillDataTable(DataTable obj, HtmlNode table) { #if NET20 var body = 0 != IEnumerableExtensionMethods.Count(table.Descendants("thead")) ? IEnumerableExtensionMethods.First(table.Descendants("tbody")) : table; var rows = IEnumerableExtensionMethods.ToList(body.Descendants("tr")); #else var body = table.Descendants("thead").Any() ? table.Descendants("tbody").First() : table; var rows = body.Descendants("tr").ToList(); #endif if (HasVerticalColumns(table)) { AddVerticalDataColumns(obj, body); AddVerticalDataRows(obj, body); return; } var columns = Columns(table); for (var i = 0; i < columns.Count; i++) { #if NET20 || NET35 var name = string.IsNullOrEmpty(columns[i]) #else var name = string.IsNullOrWhiteSpace(columns[i]) #endif ? XmlConvert.ToString(i) : columns[i]; obj.Columns.Add(name, typeof(HtmlNode)); } AddNormalDataRows(obj, rows); }
public static void Main(string[] args) { IEnumerableExtensionMethods.DataOperations(); }
public override IEnumerable <object[]> GetData(MethodInfo methodUnderTest, Type[] parameterTypes) { if (null == methodUnderTest) { throw new ArgumentNullException("methodUnderTest"); } if (null == parameterTypes) { throw new ArgumentNullException("parameterTypes"); } var list = new List <object>(); if (1 == parameterTypes.Length && parameterTypes[0] == typeof(DataSet)) { var data = new DataSet { Locale = CultureInfo.InvariantCulture }; foreach (var file in Files) { data.Tables.Add(new CsvFile(file).ToDataTable()); } list.Add(data); } else { #if NET20 if (IEnumerableExtensionMethods.Count(Files) != parameterTypes.Length) { throw new InvalidOperationException(StringExtensionMethods.FormatWith(Resources.CsvFileAttribute_CountsDiffer, IEnumerableExtensionMethods.Count(Files), parameterTypes.Length)); } #else if (Files.Count() != parameterTypes.Length) { throw new InvalidOperationException(Resources.CsvFileAttribute_CountsDiffer.FormatWith(Files.Count(), parameterTypes.Length)); } #endif var index = -1; foreach (var file in Files) { index++; if (parameterTypes[index] == typeof(CsvFile) || parameterTypes[index] == typeof(IEnumerable <KeyStringDictionary>)) { list.Add(new CsvFile(file)); continue; } if (parameterTypes[index] == typeof(DataTable)) { list.Add(new CsvFile(file).ToDataTable()); continue; } throw new InvalidOperationException(Resources.CsvFileAttribute_UnsupportedParameterType); } } yield return(list.ToArray()); }
static void Main(string[] args) { //Task 1 Console.WriteLine("Task 1"); var builder = new StringBuilder(); builder.Append("This is a test"); Console.WriteLine(builder.Substring(10, 4).ToString()); //Task 2 Console.WriteLine(); Console.WriteLine("Task 2"); var list = new List <int> { 1, 2, 3, 4, 5 }; Console.WriteLine(IEnumerableExtensionMethods.Average(list)); Console.WriteLine(IEnumerableExtensionMethods.Sum(list)); Console.WriteLine(IEnumerableExtensionMethods.Product(list)); Console.WriteLine(IEnumerableExtensionMethods.Min(list)); //Task 3 Console.WriteLine(); Console.WriteLine("Task 3"); var listOfStudents = new List <Student> { new Student("Ivan", "Petrov", "619501", "0285123285", "*****@*****.**", new List <double> { 2, 3, 5, 6, 2, 4, 5 }, new Group(2, "Physics"), 20), new Student("Gosho", "Kirov", "120125", "0885168754", "*****@*****.**", new List <double> { 3, 3, 3, 3, 3, 5, 3 }, new Group(2, "Mathematics"), 19), new Student("Pesho", "Pesheff", "748906", "0257231456", "*****@*****.**", new List <double> { 6, 6, 6, 6, 6 }, new Group(2, "Philosophy"), 31), new Student("Tosho", "Gorgiev", "365406", "6553698523", "*****@*****.**", new List <double> { 2, 2, 2 }, new Group(3, "Mathematics"), 22), new Student("Hassan", "Mehmedov", "458707", "0251236945", "*****@*****.**", new List <double> { 3, 4, 2, 5, 6, 2, 3 }, new Group(4, "Mathematics"), 19) }; var studentsWithFirstBeforeLast = from student in listOfStudents where student.FirstName.CompareTo(student.LastName) < 0 select student; foreach (var student in studentsWithFirstBeforeLast) { Console.WriteLine(student); } //Task 4 Console.WriteLine(); Console.WriteLine("Task 4"); var studentsWithAgeInInterval = from student in listOfStudents where student.Age > 17 && student.Age < 25 select student; foreach (var student in studentsWithAgeInInterval) { Console.WriteLine(student); } //Task 5 Console.WriteLine(); Console.WriteLine("Task 5"); var sortedStudents = listOfStudents.OrderBy(student => student.FirstName).ThenBy(student => student.LastName); foreach (var item in sortedStudents) { Console.WriteLine(item); } //Task 6 Console.WriteLine(); var numbers = new List <int> { 1, 2, 3, 7, 49, 21, 42, 53, 222, 149 }; Console.WriteLine("With Lambda"); DivisibleBy7And3WithLambda(numbers); Console.WriteLine("With LINQ"); DivisibleBy7And3WithLINQ(numbers); //Task 7 Console.WriteLine(); Console.WriteLine("Task 8"); var timer = new Timer(4, 3); timer.Invoke(); //Task 9 Console.WriteLine(); Console.WriteLine("Task 9"); var studentsFrom2Group = from student in listOfStudents where student.Group.GroupNumber == 2 orderby student.FirstName select student; PrintCollection(studentsFrom2Group.ToList()); //Task 10 Console.WriteLine(); Console.WriteLine("Task 10"); var studentsFrom2 = listOfStudents .Where(student => student.Group.GroupNumber == 2) .OrderBy(student => student.FirstName) .Select(student => student); PrintCollection(studentsFrom2.ToList()); //Task 11 Console.WriteLine(); Console.WriteLine("Task 11"); var studentsWithABV = from student in listOfStudents where student.Email.Contains("abv.bg") select student; PrintCollection(studentsWithABV.ToList()); //Task 12 Console.WriteLine(); Console.WriteLine("Task 12"); var sofiaMans = listOfStudents .Where(student => student.Tel.Substring(0, 2) == "02") .Select(student => student); PrintCollection(sofiaMans.ToList()); //Task 13 Console.WriteLine(); Console.WriteLine("Task 13"); var excellentStudents = listOfStudents .Where(student => student.Marks.Contains(6)) .Select(s => new { FullName = string.Format("{0} {1}", s.FirstName, s.LastName), Marks = string.Join(" ", s.Marks.ToArray()) }); PrintCollection(excellentStudents.ToList()); //Taske 14 Console.WriteLine(); Console.WriteLine("Task 14"); var badStudents = listOfStudents .Where(student => student.Marks.Where(x => x == 2).ToList().Count == 2) .Select(s => new { FullName = string.Format("{0} {1}", s.FirstName, s.LastName), Marks = string.Join(", ", s.Marks.ToArray()) }); PrintCollection(badStudents.ToList()); //Taskt 15 Console.WriteLine(); Console.WriteLine("Task 15"); var marks = listOfStudents .Where(student => student.FN.Substring(4, 2) == "06") .Select(student => student.Marks); foreach (var item in marks) { PrintCollection(item); Console.WriteLine(); } //Task 16 Console.WriteLine(); Console.WriteLine("Task 16"); var mathematicians = listOfStudents .Where(student => student.Group.DepartmentName.Equals("Mathematics")) .Select(student => student.FirstName); Console.WriteLine(string.Join(", ", mathematicians)); //Task 17 Console.WriteLine(); Console.WriteLine("Task 17"); var strings = new List <string> { "I", "am", "a", "baaaad", "string", "array" }; var longest = strings .Where(s => s.Length == (strings .Where(stringa => stringa.Length > 0) .Select(stringa => stringa.Length).Max())) .Select(s => s); PrintCollection(longest.ToList()); //Task 18 Console.WriteLine(); Console.WriteLine("Task 18"); var grouped = listOfStudents .GroupBy(student => student.Group.GroupNumber) .Select(student => student); foreach (var item in grouped) { Console.WriteLine("In Group:"); PrintCollection(item.ToList()); Console.WriteLine(); } }
private static IList <string> ColumnsHead(HtmlNode head) { #if NET20 var rows = IEnumerableExtensionMethods.ToList(head.Descendants("tr")); #else var rows = head.Descendants("tr").ToList(); #endif if (0 == rows.Count) { return(new List <string>()); } var matrix = new Matrix <string>(); foreach (var cell in rows[0].Descendants("th")) { var colspan = cell.Attributes["colspan"]; if (null == colspan) { matrix.Width++; continue; } for (var i = 0; i < XmlConvert.ToInt32(colspan.Value); i++) { matrix.Width++; } } var carry = new List <int>(); for (var i = 0; i < matrix.Width; i++) { carry.Add(0); } var y = 0; foreach (var row in rows) { var x = 0; matrix.Height++; foreach (var cell in row.Descendants("th")) { while (0 != carry[x]) { matrix[x, y] = matrix[x, y - 1]; carry[x]--; x++; } var rowspan = cell.Attributes["rowspan"]; if (null != rowspan) { carry[x] = XmlConvert.ToInt32(rowspan.Value); } var colspan = cell.Attributes["colspan"]; var name = ColumnName(cell); var index = 1; for (var i = 0; i < (null == colspan ? 1 : XmlConvert.ToInt32(colspan.Value)); i++) { matrix[x++, y] = string.Format(CultureInfo.InvariantCulture, null == colspan ? "{0}" : "{0} ({1})", name, index++); } } y++; } #if NET20 var list = new List <string>(); foreach (var element in matrix.Row(matrix.Height - 1)) { list.Add(element); } return(list); #else return(matrix.Row(matrix.Height - 1).ToList()); #endif }
static void Main(string[] args) { IEnumerableExtensionMethods.TestForEach(); }