简体   繁体   中英

'MongoDB.Driver.IFindFluent<XDocument, System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'

I am working on a asp.net core project with MongoDB.

I want to get a list from Database.

I tried this

public async Task<List<string>> GetRoomCodesFromHotelBooking(string transactionId)
{
  return HotelBookingCollection
    .Find(x => x.TransactionId == transactionId)
    .Project(x => x.Hotel.Rooms.Select(y => y.Code).ToList());
}

Note : A transactionId has more than one HotelBooking

But I got this error

Cannot implicitly convert type 'MongoDB.Driver.IFindFluent<xDocument, System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'. An explicit conversion exists (are you missing a cast?)

model class

public class HotelBookingDocument
{
    public string Id { get; set; }
    public HotelDocument Hotel { get; set; }
// removed rest 
}

public class HotelDocument
{
    public List<RoomDocument> Rooms { get; set; }
// removed rest 
}

public class RoomDocument
{
      public string Code { get; set; }
// removed rest 
}
// removed rest 

How can I select room code and make list them?

You call ToList inside Project not for a Find cursor itself, ie the result of your operation is IFindFluent<..> not a server data. In fact, you even don't call a server yet until you will apply any operation for a created find cursor like ToList , First and etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM