Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.InvalidOperationException F# snippets #7773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Enumerable1

// <Snippet6>
open System
open System.Linq

let data = [| 1; 2; 3; 4 |]
let average =
data.Where(fun num -> num > 4).Average();
printfn $"The average of numbers greater than 4 is {average}"
// The example displays the following output:
// Unhandled Exception: System.InvalidOperationException: Sequence contains no elements
// at System.Linq.Enumerable.Average(IEnumerable`1 source)
// at <StartupCode$fs>.main()
// </Snippet6>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Enumerable2

// <Snippet7>
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]
let moreThan4 =
dbQueryResults.Where(fun num -> num > 4)

if moreThan4.Any() then
printfn $"Average value of numbers greater than 4: {moreThan4.Average()}:"
else
// handle empty collection
printfn "The dataset has no values greater than 4."

// The example displays the following output:
// The dataset has no values greater than 4.
// </Snippet7>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Enumerable3

// <Snippet8>
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]

let firstNum = dbQueryResults.First(fun n -> n > 4)

printfn $"The first value greater than 4 is {firstNum}"

// The example displays the following output:
// Unhandled Exception: System.InvalidOperationException:
// Sequence contains no matching element
// at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
// at <StartupCode$fs>.main()
// </Snippet8>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Enumerable4

// <Snippet9>
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]

let firstNum = dbQueryResults.FirstOrDefault(fun n -> n > 4)

if firstNum = 0 then
printfn "No value is greater than 4."
else
printfn $"The first value greater than 4 is {firstNum}"

// The example displays the following output:
// No value is greater than 4.
// </Snippet9>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Enumerable5

// <Snippet10>
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]

let singleObject = dbQueryResults.Single(fun value -> value > 4)

// Display results.
printfn $"{singleObject} is the only value greater than 4"

// The example displays the following output:
// Unhandled Exception: System.InvalidOperationException:
// Sequence contains no matching element
// at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
// at <StartupCode$fs>.main()
// </Snippet10>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Enumerable6

// <Snippet11>
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]

let singleObject = dbQueryResults.SingleOrDefault(fun value -> value > 2)

if singleObject <> 0 then
printfn $"{singleObject} is the only value greater than 2"
else
// Handle an empty collection.
printfn "No value is greater than 2"

// The example displays the following output:
// Unhandled Exception: System.InvalidOperationException:
// Sequence contains more than one matching element
// at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
// at <StartupCode$fs>.main()
// </Snippet11>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Iterating1

// <Snippet1>
open System

let numbers = ResizeArray [| 1; 2; 3; 4; 5 |]
for number in numbers do
let square = Math.Pow(number, 2) |> int
printfn $"{number}^{square}"
printfn $"Adding {square} to the collection...\n"
numbers.Add square

// The example displays the following output:
// 1^1
// Adding 1 to the collection...
//
//
// Unhandled Exception: System.InvalidOperationException: Collection was modified
// enumeration operation may not execute.
// at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
// at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
// at <StartupCode$fs>.main()
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Iterating2

// <Snippet2>
open System
open System.Collections.Generic

let numbers = ResizeArray [| 1; 2; 3; 4; 5 |]

let upperBound = numbers.Count - 1
for i = 0 to upperBound do
let square = Math.Pow(numbers[i], 2) |> int
printfn $"{numbers[i]}^{square}"
printfn $"Adding {square} to the collection...\n"
numbers.Add square

printfn "Elements now in the collection: "
for number in numbers do
printf $"{number} "
// The example displays the following output:
// 1^1
// Adding 1 to the collection...
//
// 2^4
// Adding 4 to the collection...
//
// 3^9
// Adding 9 to the collection...
//
// 4^16
// Adding 16 to the collection...
//
// 5^25
// Adding 25 to the collection...
//
// Elements now in the collection:
// 1 2 3 4 5 1 4 9 16 25
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Iterating3

// <Snippet3>
open System
open System.Collections.Generic

let numbers = ResizeArray [| 1; 2; 3; 4; 5 |]
let temp = ResizeArray()

// Square each number and store it in a temporary collection.
for number in numbers do
let square = Math.Pow(number, 2) |> int
temp.Add square

// Combine the numbers into a single array.
let combined = Array.zeroCreate<int> (numbers.Count + temp.Count)
Array.Copy(numbers.ToArray(), 0, combined, 0, numbers.Count)
Array.Copy(temp.ToArray(), 0, combined, numbers.Count, temp.Count)

// Iterate the array.
for value in combined do
printf $"{value} "
// The example displays the following output:
// 1 2 3 4 5 1 4 9 16 25
// </Snippet3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module List_Sort1

// <Snippet12>
type Person(firstName: string, lastName: string) =
member val FirstName = firstName with get, set
member val LastName = lastName with get, set

let people = ResizeArray()

people.Add(Person("John", "Doe"))
people.Add(Person("Jane", "Doe"))
people.Sort()
for person in people do
printfn $"{person.FirstName} {person.LastName}"
// The example displays the following output:
// Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. --->
// System.ArgumentException: At least one object must implement IComparable.
// at System.Collections.Comparer.Compare(Object a, Object b)
// at System.Collections.Generic.ArraySortHelper`1.SwapIfGreater(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
// at System.Collections.Generic.ArraySortHelper`1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer, Int32 depthLimit)
// at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
// --- End of inner exception stack trace ---
// at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
// at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
// at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
// at <StartupCode$fs>.main()
// </Snippet12>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module List_Sort2

// <Snippet13>
open System

type Person(firstName: string, lastName: string) =
member val FirstName = firstName with get, set
member val LastName = lastName with get, set

interface IComparable<Person> with
member this.CompareTo(other) =
compare $"{this.LastName} {this.FirstName}" $"{other.LastName} {other.FirstName}"

let people = ResizeArray()

people.Add(new Person("John", "Doe"))
people.Add(new Person("Jane", "Doe"))
people.Sort()
for person in people do
printfn $"{person.FirstName} {person.LastName}"
// The example displays the following output:
// Jane Doe
// John Doe
// </Snippet13>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module List_Sort3

// <Snippet14>
open System
open System.Collections.Generic

type Person(firstName, lastName) =
member val FirstName = firstName with get, set
member val LastName = lastName with get, set

type PersonComparer() =
interface IComparer<Person> with
member _.Compare(x: Person, y: Person) =
$"{x.LastName} {x.FirstName}".CompareTo $"{y.LastName} {y.FirstName}"

let people = ResizeArray()

people.Add(Person("John", "Doe"))
people.Add(Person("Jane", "Doe"))
people.Sort(PersonComparer())
for person in people do
printfn $"{person.FirstName} {person.LastName}"
// The example displays the following output:
// Jane Doe
// John Doe
// </Snippet14>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module List_Sort4

// <Snippet15>
open System
open System.Collections.Generic

type Person(firstName, lastName) =
member val FirstName = firstName with get, set
member val LastName = lastName with get, set

let personComparison (x: Person) (y: Person) =
$"{x.LastName} {x.FirstName}".CompareTo $"{y.LastName} {y.FirstName}"

let people = ResizeArray()

people.Add(Person("John", "Doe"))
people.Add(Person("Jane", "Doe"))
people.Sort personComparison
for person in people do
printfn $"{person.FirstName} {person.LastName}"

// The example displays the following output:
// Jane Doe
// John Doe
// </Snippet15>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Nullable1

// <Snippet4>
open System
open System.Linq

let queryResult = [| Nullable 1; Nullable 2; Nullable(); Nullable 4 |]
let map = queryResult.Select(fun nullableInt -> nullableInt.Value)

// Display list.
for num in map do
printf $"{num} "
printfn ""
// The example displays the following output:
// 1 2
// Unhandled Exception: System.InvalidOperationException: Nullable object must have a value.
// at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
// at Example.<Main>b__0(Nullable`1 nullableInt)
// at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
// at <StartupCode$fs>.main()
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Nullable2

// <Snippet5>
open System
open System.Linq

let queryResult = [| Nullable 1; Nullable 2; Nullable(); Nullable 4 |]
let numbers = queryResult.Select(fun nullableInt -> nullableInt.GetValueOrDefault())

// Display list using Nullable<int>.HasValue.
for number in numbers do
printf $"{number} "
printfn ""

let numbers2 = queryResult.Select(fun nullableInt -> if nullableInt.HasValue then nullableInt.Value else -1)
// Display list using Nullable<int>.GetValueOrDefault.
for number in numbers2 do
printf $"{number} "
printfn ""
// The example displays the following output:
// 1 2 0 4
// 1 2 -1 4
// </Snippet5>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Iterating1.fs" />
<Compile Include="Iterating2.fs" />
<Compile Include="Iterating3.fs" />
<Compile Include="List_Sort1.fs" />
<Compile Include="List_Sort2.fs" />
<Compile Include="List_Sort3.fs" />
<Compile Include="List_Sort4.fs" />
<Compile Include="Nullable1.fs" />
<Compile Include="Nullable2.fs" />
<Compile Include="Enumerable1.fs" />
<Compile Include="Enumerable2.fs" />
<Compile Include="Enumerable3.fs" />
<Compile Include="Enumerable4.fs" />
<Compile Include="Enumerable5.fs" />
<Compile Include="Enumerable6.fs" />
</ItemGroup>
</Project>
Loading