C# linq 返回重复数据
我有一个这样的 linq 查询:
var fling = (from b in flowering.FlowerViews
where ((!string.IsNullOrEmpty(flow_name)) && b.FLOWER_NAME == flow_name) || flow_name==""
where ((!string.IsNullOrEmpty(color_name)) && b.COLOR_NAME == color_name) || color_name == ""
where ((!string.IsNullOrEmpty(size)) && b.FLOWER_SIZE == size) || size==""
where ((low_price!=0) && low_price<= b.FLOWER_PRICE) || low_price==0
where ((high_price!=0) && high_price >= b.FLOWER_PRICE) || high_price==0
orderby b.COLOR_NAME
select new { b.FLOWER_NAME, b.COLOR_NAME, b.FLOWER_SIZE, b.FLOWER_PRICE, b.CHAR_DESC});
我的 where 子句对我有用,但是当我对返回值运行 for every 循环时,会出现重复数据,因为 b.CHAR_DESC 有 3 个值,而所有其他返回数据只有一个值。我想知道是否有一种方法可以将分配给 b.CHAR_DESC 的 3 个值放入一个不会导致重复的 b.Flower_name 出现的结构中
I have a this linq query:
var fling = (from b in flowering.FlowerViews
where ((!string.IsNullOrEmpty(flow_name)) && b.FLOWER_NAME == flow_name) || flow_name==""
where ((!string.IsNullOrEmpty(color_name)) && b.COLOR_NAME == color_name) || color_name == ""
where ((!string.IsNullOrEmpty(size)) && b.FLOWER_SIZE == size) || size==""
where ((low_price!=0) && low_price<= b.FLOWER_PRICE) || low_price==0
where ((high_price!=0) && high_price >= b.FLOWER_PRICE) || high_price==0
orderby b.COLOR_NAME
select new { b.FLOWER_NAME, b.COLOR_NAME, b.FLOWER_SIZE, b.FLOWER_PRICE, b.CHAR_DESC});
my where clauses work for me but when I run a for each loop over the returned values there is duplicate data because b.CHAR_DESC has 3 values to it where all the other return data only have one. I am wondering if there is a way to get the 3 values assigned to b.CHAR_DESC into a structure that does not cause duplicate b.Flower_name's to show up
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据这篇文章,您应该能够为匿名类型调用 Distinct()
并且编译器将根据属性值处理匿名类型的
GetHashCode()
和Equals()
。Based on this post you should be able to call Distinct() for the anonymous type
And the compiler will take care of
GetHashCode()
andEquals()
for the anonymous type based on attribute values.在 select 子句末尾的最后一个括号后面添加
.Distinct()
。Add
.Distinct()
at the end of your select clause, after the final parenthesis.