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 8359977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:19:15+00:00 2026-06-09T11:19:15+00:00

Basically I want to adapt this code for LINQ: private Tile CheckCollision(Tile[] tiles) {

  • 0

Basically I want to adapt this code for LINQ:

private Tile CheckCollision(Tile[] tiles)
{
    foreach (var tile in tiles)
    {
        if (tile.Rectangle.IntersectsWith(Rectangle))
        {
            return tile;
        }
    }

    return null;
}

The code checks each tile and returns the first tile that collides with the object. I only want the first tile, not an array of tiles like I would get if I use this:

private Tile CheckCollision(Tile[] tiles)
{
    var rtn = 
        from tile in tiles
        where tile.Rectangle.IntersectsWith(Rectangle)
        select tile;

}

What should I do?

  • 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-09T11:19:18+00:00Added an answer on June 9, 2026 at 11:19 am

    You could use the .First() or .FirstOrDefault() extension method that allows you to retrieve the first element matching a certain condition:

    private Tile CheckCollision(Tile[] tiles)
    {
        return tiles.FirstOrDefault(t => t.Rectangle.IntersectsWith(Rectangle));
    }
    

    The .First() extension method will throw an exception if no element is found in the array that matches the required condition. The .FirstOrDefault() on the other hand will silently return null. So use the one that better suits your needs.

    Notice that there’s also the .Single() extension method that you could use. The difference with .First() is that .Single() will throw an exception if more than one elements matches the condition whereas .First() will return the first one.

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

Sidebar

Related Questions

I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
I basically want to perform the same functions as this code, but in C.
I basically want to do this: foreach my $key (keys $hash_ref) { Do stuff
I basically want to do this: SELECT HasComments = CASE (LEN(Comments) > 1) WHEN
I basically want to autodetect a user's timezone using Rails. I can use this
I basically want to do this: <% @videos.each do |vid| %> <div id=vid.location> ...
I basically want to make this in my application, so our technicians have an
Basically I want to make this: <li id=about><a href=#>About</a> Into this when the page
I basically want to display a rectangle on a dialog window widget. Using another
I basically want a Python equivalent of this Array in C: int a[x]; but

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.