linq to entity distinct

hcb111 2012-10-26 11:06:16
现在有这样的语句
select value c from AAA as c where 一堆条件 order by id

我的需求是,执行出来的结果又重复项,我想distinct掉,linq to entity中不知道该怎么写。

翻译成sql语句应该是这样

select * from AAA as a where id=(select min(id) from BBB where name=a.name)

...全文
303 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
你的选择B 2012-11-06
  • 打赏
  • 举报
回复

/// <summary>
        /// 返回不重复的Customers
        /// </summary>
        public static List<Customers> DistinctSearch()
        {
            List<Customers> customers = null;
            using (NorthwindEntities entity = new NorthwindEntities())
            {
                //以NorthWind为例,查找地区为London,并按照CustomerID升序,且不重复的记录
                customers = entity.Customers.Where(c => c.City == "London").OrderBy(n => n.CustomerID).Distinct().ToList();
            }

            return customers;
        }
EnForGrass 2012-11-03
  • 打赏
  • 举报
回复

var query=(from a in db.AAA 
          where a.id== db.BBB.Where(b=>b.name==a.name)
          select a).Distinct();
cass8816 2012-11-02
  • 打赏
  • 举报
回复
var q = (from a in b
select a).distinct()
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

var query=from a in db.AAA
where a.id== db.BBB.Where(b=>b.name==a.name).Select(b=>b.id).Min()
select a;
[/Quote]去从就加.distinct()
lhx527099095 2012-10-26
  • 打赏
  • 举报
回复
楼上正解
q107770540 2012-10-26
  • 打赏
  • 举报
回复
var query=from a in db.AAA
where a.id== db.BBB.Where(b=>b.name==a.name).Select(b=>b.id).Min()
select a;
hcb111 2012-10-26
  • 打赏
  • 举报
回复
select * from AAA as a where id=(select min(id) from BBB where name=a.name)

就这句sql翻译成linq to entity,该怎么写?
  • 打赏
  • 举报
回复
where(x=>x.字段名==值)

实在搞不定就下载个转换软件 linq和sql能转换
hcb111 2012-10-26
  • 打赏
  • 举报
回复
因为我这个写法不是标准的LINQ语句

平时写成
select value c from AAA as c where 一堆条件


怎么样像在SQL里一样,只需要在WHERE条件中加入一段话,那段话该怎么写
  • 打赏
  • 举报
回复
你的问题里面没有distinct操作

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧