//存货编码不以x开头的则是非修磨刀具 var q = from s in db.Inventory.Where(s => s.cInvCCode.StartsWith("9")) where !s.cInvCode.ToUpper().StartsWith("X")//此句很关键,采用StringComparison.OrdinalIgnoreCase功能去忽略大小写会报错 select new { 存货编码 = s.cInvCode, 存货名称 = s.cInvName, 规格型号 = s.cInvStd, 建档日期 = s.dSDate }; //查找重复项 var q1 = from s in db.Inventory.Where(s => s.cInvCCode.StartsWith("9")) where !s.cInvCode.ToUpper().StartsWith("X") group s by s.cInvStd into g where g.Count() > 1 select new { 规格型号 = g.Key, 重复个数 = g.Count() }; var qz = from s1 in q join s2 in q1 on s1.规格型号 equals s2.规格型号 orderby s1.规格型号 , s1.建档日期 descending select new { s1.存货编码, s1.存货名称, s1.规格型号, s2.重复个数,s1.建档日期 }; dataGridView1.DataSource = qz.ToList(); dataGridView1.AutoResizeColumns();