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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:08:08+00:00 2026-05-28T03:08:08+00:00

I have this list: Annie – P May – Annie – P May –

  • 0

I have this list:

Annie – P
May – ” ”
Annie – P
May – P

And I want to get the last element based on the names:

Annie – P
May – P

I have this code but it’s either throwing an error: The type arguments for method 'System.Linq.Enumerable.SelectMany<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,System.Collections.Generic.IEnumerable<TResult>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. or I only get the very last element in the list (May – P).

class Passenger
{
    public string Name { get; set; }
    public int PassengerID { get; set; }
    public List<TravelDoc> TravelDocs { get; set; }
}

class TravelDoc
{
    public string DocType { get; set; }
    public int? DocNumber { get; set; }
}

List<Passenger> modList = new List<Passenger>()
    {

        new Passenger() { Name = "Annie", PassengerID = 0, 
                          TravelDocs = new List<TravelDoc>()
                          {
                            new TravelDoc() { DocNumber = 100, DocType = "P" }
                          }
                        },
        new Passenger() { Name = "May", PassengerID = 1, 
                          TravelDocs = new List<TravelDoc>()
                          {
                            new TravelDoc() { DocNumber = null, DocType = "" }
                          }
                        },      
        new Passenger() { Name = "Annie", PassengerID = 0, 
                          TravelDocs = new List<TravelDoc>()
                          {
                            new TravelDoc() { DocNumber = 100, DocType = "P" }
                          }
                        },
        new Passenger() { Name = "May", PassengerID = 1, 
                          TravelDocs = new List<TravelDoc>()
                          {
                            new TravelDoc() { DocNumber = 200, DocType = "P" }
                          }
                        }
    };      

Code (Throws an error)

var passengersMod = modList.SelectMany(pax => pax.TravelDocs         
                    .Select(doc => new { Passenger = pax, TravelDoc = doc })
                    .Last())
                    .Dump();

Code (Wrong Results)

var passengersMod = modList.SelectMany(pax => pax.TravelDocs         
                    .Select(doc => new { Passenger = pax, TravelDoc = doc })
                    ).Last()
                    .Dump();

Note: Dump() is a LINQPad extension. 😀

How can I get the result I wanted using LINQ?

Thanks in advance.

Edit:

Okay, it seems that most people gets confused with “based on names” condition. Sorry about that but unfortunately I could not think of any better words to describe the criteria (sorry). But Jon Hanna got what I meant, so thanks! Also I removed the “of anonymous types” from the title as suggested. Thanks again! 😀

Another Edit:

I have another situation similar to this with slight difference. Yes, the list was group according to some criteria (in this example by names) but instead of needing the last item from each grouping, what I need is the nth item from each grouping.

I was able to get the nth from each grouping:

modList.GroupBy(p => p.Name).Select(g => g.ToList()[index])

Years after I asked this question, Jon’s answer still helped me. How I wish I could upvote his answer twice! I hope this might help somebody else. 😀

  • 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-05-28T03:08:09+00:00Added an answer on May 28, 2026 at 3:08 am

    Why not ignore anonymous types and SelectMany and just do:

    modList.GroupBy(p => p.Name).Select(g => g.Last())
    

    It’s got nothing to do with anonymous types as per your title, but it does result in the sort of results you mention at the start of the question.

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

Sidebar

Related Questions

I have this list: names = ['john','Jonh','james','James','Jardel'] I want loop over the list and
So i have this ArrayList: list.get(0) == love list.get(1) == foo list.get(2) == make
i have this code: var list = new List<int>(); for(int i=0;i<10;i++) list.Add(i); for(int i=0;i<10;i++)
I have a collection like this List<int> {1,15,17,8,3}; how to get a flat string
I have this code for splitting input list into its halves. It seems to
I have this situation where I want to display a list of Administration objects
i have this problem: starting from an empty list (0 elements) i want check
I have this list in python: fileTypesToSearch = ['js','css','htm', 'html'] and I want to
I have this code: List<int> list = new List<int>(); for (int i = 1;
I have this list of dictionary below. What i want is to search for

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.