2011-05-10 58 views
0

我有这个LINQ查询:C#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 each循环在返回的值有重复的数据,因为b.CHAR_DESC有3个值的所有其他返回数据只有一个。我想知道是否有办法将分配给b.CHAR_DESC的3个值分配到不会导致重复b.Flower_name出现的结构中

+0

您能否提供您当前输出的样本。你想得到一个输出样本吗? – 2011-05-10 01:43:18

回答

5

基于this post,你应该能够调用鲜明的()的匿名类型

var list = fling.Distinct().ToList(); 

,编译器会照顾GetHashCode()Equals()基于属性值的匿名类型。

5

.Distinct()添加到select子句的末尾,在最后一个括号之后。