Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用linq到对象distinct将distinct数据绑定到中继器_C#_Linq_Distinct - Fatal编程技术网

C# 使用linq到对象distinct将distinct数据绑定到中继器

C# 使用linq到对象distinct将distinct数据绑定到中继器,c#,linq,distinct,C#,Linq,Distinct,我试图根据不同的数据过滤记录。我使用了以下代码 List<BALHotelList> searchresult = (from a in bh join b in hr on a.HotelCode equals b.hotelCode orderby a.HotelName

我试图根据不同的数据过滤记录。我使用了以下代码

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();

您已经在此处选择了
HotelArea

var uniqueArea = searchresult.Select(m => m.HotelArea).Distinct();
。。。因此,在数据绑定中,您应该只选择值本身


如果您需要数据源中的全部信息,而不仅仅是酒店区域的信息,那么您需要一些类似于
的信息来区分这些信息。

好的,我把答案移到这里了

第1部分:

第2部分: 跳过空字符串和空字符串:

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

是的,当我调试distinct HotelArea时,var uniqueArea会显示字符串列表,但当我将其绑定到转发器数据时,它会失败source@rahularyansharma:是的,因为我确信在代码中您没有显示使用,您正在指定一个绑定,该绑定尝试在每个值中查找名为HotelArea的属性。我编辑了代码以显示uniqueArea的使用,请选中“已编辑”part@rahularyansharma:右-查看
Eval(“HotelArea”)
位-这就是试图查找
HotelArea
部分的内容。到那时,你已经得到了字符串;您只需要值本身…如何修改我的this子句var uniqueArea=searchresult.Select(m=>m.HotelArea.Distinct();要在datasource中获得HotelArea以便repeater工作,请查看此问题的答案:.@Serge+1其工作。还有一件事,在不同的情况下null是否被视为唯一的是,我想跳过null值var uniqueArea=searchresult.Select(m=>m.HotelArea).Where(m=>m!=null.distinct();那empety弦呢?请将您的评论作为答案发布,以便我可以接受。这比乔恩的回答更接近
var uniqueArea =searchresult.Select(m => m.HotelArea).Where(m => !string.IsNullOrEmpty(m)).Distinct();