Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:00:45+00:00 2026-06-15T02:00:45+00:00

Below my code. It returns exception InvalidCastException. And main question is – why? What

  • 0

Below my code. It returns exception “InvalidCastException”. And main question is – why?
What is wrong?

Error text:

Unable to cast object of type
‘WhereSelectListIterator`2[Monopolowy_beta.Gracz,Monopolowy_beta.Gracz]’
to type ‘Monopolowy_beta.Gracz’.

namespace Monopolowy_beta
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Gracz> lista = new List<Gracz> { };
            Gracz g1 = new Gracz();
            Gracz g2 = new Gracz();
            Gracz g3 = new Gracz();
            g2.Id = 3;

            lista.Add(g1);
            lista.Add(g2);
            lista.Add(g3);

            g1 = GraczeTools.UstawAktywnegoGracza(lista, 3);


            Console.ReadKey();
        }
    }
}

Error in these lines:
var docelowy = from item in listagraczy where (item.Id==ID && item.czyAktywny == true) select listagraczy[listagraczy.IndexOf(item) + 1];
gracz = (Gracz)docelowy;

namespace Monopolowy_beta
{
    static class GraczeTools
    {

        public static Gracz UstawAktywnegoGracza(List<Gracz> listagraczy, int ID)
        { 
            Gracz gracz = new Gracz();
            if (ID == 4){
                var docelowy = from item in listagraczy where (item.czyAktywny == true && item.Id == 3) select listagraczy[1];
                gracz = (Gracz)docelowy;
            }

            if (ID != 4){
                var docelowy = from item in listagraczy where (item.Id==ID && item.czyAktywny == true) select listagraczy[listagraczy.IndexOf(item) + 1];
                gracz = (Gracz)docelowy;
            }


            return gracz;
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:00:46+00:00Added an answer on June 15, 2026 at 2:00 am
    var docelowy = from item in listagraczy 
                   where (item.czyAktywny == true && item.Id == 3) 
                   select listagraczy[1];
    

    Let’s examine this query. It finds all items which satisfy condition (yes, it will return sequence, not single item) and for each such item, it returns.. second element of listagraczy list. Yes, you don’t have items, which matched your condition.

    I think you should select item instead (this a range variable of your query), and apply FirstOrDefault to result, because by default query will return IEnumerable<Gracz> result.

    var docelowy = (from item in listagraczy 
                    where (item.czyAktywny == true && item.Id == 3) 
                    select item).FirstOrDefault();
    

    Which is better to write with fluent API:

    var docelowy = listagraczy.FirstOrDefault(item => item.czyAktywny && item.Id == 3);
    

    Also you can use boolean values directly in conditions (i.e. item.czyAktywny instead of item.czyAktywny == true).


    After little refactoring your method should look like

    public static Gracz UstawAktywnegoGracza(List<Gracz> listagraczy, int ID)
    { 
       return listagraczy
               .FirstOrDefault(item => item.Id == 3 && (ID != 4 || item.czyAktywny));
    }
    

    How it works:

    You have two conditional blocks in your method if (ID == 4) and if (ID != 4) (which is actually if ... else. Difference is that you are filtering sequence by one more condition in first case – item.czyAktywny should be true. In second case this property does not matter. So, you can add one filtering condition instead (ID != 4 || item.czyAktywny) – czyAktywny will be verified only if ID equal to 4. Also you don’t need to create new Gracz object in your method, because you anyway return one from passed list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below returns the 10 most recent entries to a MySQL database. That's
The code below returns the number of resolved tickets and the number of opened
HI: Below code is from RequestFactoryEditorDriver: /** * Returns a new array containing the
The code below correctly returns the XML from the soap sever that I'm accessing.
In the code below, the echo at the top returns true, but the echo
Right now the code below tests for a blank text box. If it is
the code below returns the fields of a given table (Employee), but I need
I have code below which has been working on devices of type Windows Mobile
I use the code below and get this error InvalidDataExcption: The magic number in
I have the below code which goes through and returns disk information. When running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.