LINQ 去除重复的数据问题
各位大家好。
var d1 = from p in tr.考试记录表
from pp in tr.考题分类表
where p.考题分类 == pp.id
where p.考试分数<100
where p.用户id == Common.Get_UserID
orderby p.id descending
select new { pp.考题分类名称, p.考试分数, p.id,p.答题个数,p.考题分类 };
问一个问题啊。考题分类有重复的。我想把重复的数据去掉,用.Distinct 但是不会写啊。
求解答啊。
var userlist=(from user in dc.Users
where user.name.contains(key)
select user).Distinct();
这是查询单项,但是我查询出来很多项啊。
[最优解释]
给你个扩展方法
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element))) { yield return element; }
}
}
ar d1 = (from p in tr.考试记录表
from pp in tr.考题分类表
where p.考题分类 == pp.id
where p.考试分数<100
where p.用户id == Common.Get_UserID
orderby p.id descending
select new { pp.考题分类名称, p.考试分数, p.id,p.答题个数,p.考题分类 }).DistinctBy(p=>
new { p.考题分类,p. 答题个数});
[其他解释]
看下帮助文档,里面有例子,看完后自己试下,如果不成功, 再问~~~
http://msdn.microsoft.com/zh-cn/library/bb338049.aspx
[其他解释]
我是两个表查询啊。不怎么会改。
[其他解释]
我是两个表查询啊。不怎么会改。
[其他解释]
Distinct方法有个重载的方法
这才是你所想要的
参考:
http://blog.csdn.net/q107770540/article/details/5784646
[其他解释]
select distinct(d.rolename),(a.operator),(d.id) from MANAGE_PROCESS这个对你应该有用,查询三列的,我的是oracle
[其他解释]
后面还有些left join没发出来,不过对distinct没影响
[其他解释]