Unable to launch object

I'm new to LINQ, I tried to run the following code and I got an InvalidCastException error: "Unable to overlay an object of type 'd__3a`1 [debug.Product]" on type "debug.Product" "- what's wrong?

Code (VB - Using VS2008)

    Private Sub btnLinq_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLinq.Click
        Dim Products As New List(Of Product)

        Dim p1 As New Product
        p1._ID = "1"
        p1._Name = "Product A"
        Products.Add(p1)

        Dim p2 As New Product
        p2._ID = "2"
        p2._Name = "Product B"
        Products.Add(p2)

        Dim p3 As New Product
        p3._ID = "3"
        p3._Name = "Product C"
        Products.Add(p3)

        Dim prod As Product = From p In Products Where p._ID = "1" Select p Take 1
        MsgBox(prod._ID)

    End Sub
End Class

Public Class Product
    Public _ID As String
    Public _Name As String
End Class

      

+2


source to share


2 answers


Take returns IEnumerable<Product>

(in your case), not the Product. (Check it out by outputting result.GetType ()): (note that my example code is in C #)

        List<Product> products = new List<Product> ();

        products.Add (new Product ()
        {
            Id = 1,
            Name = "Prod1"
        });

        products.Add (new Product ()
        {
            Id = 2,
            Name = "Prod2"
        });


        var result = ( from p in products
                       where p.Id == 1
                       select p ).Take (1);

        Console.WriteLine (result.GetType ());

        Console.ReadLine ();

      

In my case, the above code outputs:

System.Linq.Enumerable+<TakeIterator>d__3a`1[LinqTest.Product]

      



In your case, instead of using Take 1, you can try using First or FirstOrDefault.

So try this:

        var result = ( from p in products
                       where p.Id == 1
                       select p ).FirstOrDefault ();

        Console.WriteLine (result.GetType ());
        Console.WriteLine (result.Name);

      

+11


source


Method Take

( Enumerable.Take

) does not return an element, but returns another sequence ( IEnumerable<Product>

). Take(n)

just creates a new sequence with at most n elements.

To take the first element in a sequence, use one of the following methods:



  • FirstOrDefault()

    - returns null

    if sequence is empty
  • First()

    - throws InvalidOperationException

    if sequence is empty
+1


source







All Articles