首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

LINQ 去除重复的数据有关问题

2012-12-14 
LINQ去除重复的数据问题各位大家好。 var d1 from p in tr.考试记录表from pp in tr.考题分类表where p.

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. 答题个数});

[其他解释]
var resultCom = (fmsDb.Company_Investment.Where(c=>c.CompanyID==companyID).Select(c=>c.WebSite)).Distinct();
你可以这样试试看    select里面写你要查询的字段


[其他解释]
看下帮助文档,里面有例子,看完后自己试下,如果不成功, 再问~~~
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没影响
[其他解释]

引用:
Distinct方法有个重载的方法
这才是你所想要的
参考:

http://blog.csdn.net/q107770540/article/details/5784646

不错 收藏下先。
[其他解释]
谢谢各位,对我很有用
[其他解释]
看帖还是 要看兔子党执行督察的
[其他解释]
谢谢各位的答复

热点排行