DAL.DALDataContext dataContext = new DAL.DALDataContext();
            var matchEntryId = from u in dataContext.EntryKeywordMenu
                               join o in dataContext.Keywords
                               on u.KeywordID equals o.KeywordID 
                               where keywordList.Contains(o.keywordName)
                               select new { entryId = u.EntryID };
以上代码返回的entryId包含重复值,我不想写多余的代码去判重,如何改写才能去重呢?

解决方案 »

  1.   

    http://msdn.microsoft.com/zh-cn/library/system.linq.queryable.distinct.aspx
      

  2.   

    DAL.DALDataContext dataContext = new DAL.DALDataContext();
                var matchEntryId =(from u in dataContext.EntryKeywordMenu
                                   join o in dataContext.Keywords
                                   on u.KeywordID equals o.KeywordID 
                                   where keywordList.Contains(o.keywordName)
                                   select new { entryId = u.EntryID }).Distinct();
      

  3.   

    注意 Distinct 非延迟
      

  4.   

    .Distinct()
      

  5.   

    例如像 WHERE  SELECT 这些操作符 是延迟查询操作符
    在查询数据库时 不会立即去查 只会在你需要使用这些数据时才会去查询  这就是所谓的 LINQ延迟特性但是 DISTICT  TOLIST等等这些操作符 是非延迟操作符
    他会在代码执行到它们那时 立即去查询数据库 将数据加载到内存中 破坏了LINQ的延迟特性
      

  6.   

    就对取出来的matchEntryId 进行一次Distinct()嘛var result = matchEntryId.Distinct();