1

I am trying to filter records basis on distinct . I used the following code

List<BALHotelList> searchresult = (from a in bh
                                           join b in hr on a.HotelCode equals b.hotelCode
                                           orderby a.HotelName
                                           select new BALHotelList
                                               {
                                                   HotelCode = a.HotelCode,
                                                   ImageURL_Text = a.ImageURL_Text,
                                                   HotelName = a.HotelName,
                                                   StarRating = a.StarRating,
                                                   HotelAddress = a.HotelAddress,
                                                   Destination = a.Destination,
                                                   Country = a.Country,
                                                   HotelInfo = a.HotelInfo,
                                                   Latitude = a.Latitude,
                                                   Longitude = a.Longitude,
                                                   HotelArea=a.HotelArea,
                                                   totalPrice = b.totalPrice,
                                                   totalPriceSpecified = b.totalPriceSpecified,
                                                   totalSalePrice = b.totalSalePrice,
                                                   totalSalePriceSpecified = b.totalSalePriceSpecified,
                                                   rooms = b.rooms,
                                                   boardType = b.boardType

                                               }).ToList();

        var uniqueArea =searchresult.Select(m => m.HotelArea).Distinct();


  rptHotelArea.DataSource = uniqueArea;
        rptHotelArea.DataBind();

but it's not finding HotelArea in data source .

but when i debug its shows me all distinct values in uniqueArea

the error is as following:

DataBinding: 'System.String' does not contain a property with the name 'HotelArea'.

EDITED

Here is repeater HTML

  <asp:Repeater ID="rptHotelArea" runat="server">
        <ItemTemplate>
            <div class="sub-part1">
                <a href="#"><%#Eval("HotelArea")%></a></div>
        </ItemTemplate>
    </asp:Repeater>
Cœur
  • 37,241
  • 25
  • 195
  • 267
rahularyansharma
  • 11,156
  • 18
  • 79
  • 135

2 Answers2

1

You've already selected HotelArea here:

var uniqueArea = searchresult.Select(m => m.HotelArea).Distinct();

... so in your databinding you should just select the value itself.

If you need the whole information within the data source, rather than just the hotel area, you'd need something like DistinctBy from MoreLINQ.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • yes, and var uniqueArea show me the list of string when i debug of distinct HotelArea , but it fails when i bind this to repeater data source – rahularyansharma Jul 30 '12 at 08:37
  • @rahularyansharma: Yes, because I'm sure in the code you haven't shown use, you're specifying a binding which tries to find a property called HotelArea *within* each value. – Jon Skeet Jul 30 '12 at 08:40
  • i edited my code to show use of uniqueArea, Please check edited part – rahularyansharma Jul 30 '12 at 08:41
  • @rahularyansharma: Right - see that `Eval("HotelArea")` bit - that's what's trying to find the `HotelArea` part. By that point you've already got the string; you just need the value itself... – Jon Skeet Jul 30 '12 at 08:44
  • how can i modify my this clause var uniqueArea =searchresult.Select(m => m.HotelArea).Distinct(); to get HotelArea in datasource so repeater works – rahularyansharma Jul 30 '12 at 08:46
  • thanks , i got answer from @Serge comment i changed the repeater HTML like this <%#Container.DataItem%> and its works now. I want to know one more thing will distinct treat null as unique element – rahularyansharma Jul 30 '12 at 08:50
  • @rahularyansharma: Yes, that's a separate value. – Jon Skeet Jul 30 '12 at 09:01
1

Okay, I've moved my answers here.

Part 1: ASP.NET Repeater bind List<string>

Part 2: Skip null and empty strings:

var uniqueArea =searchresult.Select(m => m.HotelArea).Where(m => !string.IsNullOrEmpty(m)).Distinct();
Community
  • 1
  • 1