我只需要独特的结果 [英] I just need the distinct results

查看:95
本文介绍了我只需要独特的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将其转换为不同的查询.到目前为止,所有努力都失败了

I need to convert this to a distinct query. So far all efforts have failed

string Agency
var query = (
   from c in context.tEmp
   where 
      (c.Agency.Equals(Agency))
   orderby c.Division
   select new
   {
      OrgName = c.Division
   }
);



这会多次返回每个分区,我只需要不同的分区列表.
在SQL Server中将是这样:



This returns each division multiple times, I only want the distinct list of divisions.
In SQL Server it would be this:

SELECT
   DISTINCT   division
FROM tEmp
WHERE   Agency= @Agency



我尝试了var query =(…).distinct和其他各种方法,例如第二个var queryDistinct =(…从查询中的c…);

我知道有一个简单的方法,任何人都可以帮忙吗?
谢谢!

编辑8/27/2012-快速澄清:(以及过于简化)

我要编写三个Web服务调用:



I''ve tried var query = ( … ) .distinct and various other things like a second var queryDistinct = ( … from c in query …);

I know there''s a simple way, can anyone help?
Thanks!

Edit 8/27/2012 - Quick clarification: (and over-simplification)

I have three web service calls to write:

public List<Org> GetAgencies()
public List<Org> GetDivisionsInAgency(string Agency)
public List<Emp> EmployeeSearchByNameAgencyDivision(string EmpName, string Agency, string Division)



因此,是的,在查询 do 中,我想返回不同的部门(在给定的机构内).
我为什么要这样做?因为我不控制输入数据,所以部门名称(以及机构/部门/小组/部门/部门)可以随时更改.通过这样做,我希望使用该服务的任何人都可以调试为什么他们在不打电话给我的情况下不再能获得其代理机构/部门的雇员名单(无论他们愿意与否).

这有帮助吗?我想我简化得太多了(看起来好像是一个错误)

感谢您到目前为止的回答,我将开始研究它们,并会指出哪些是可行的.
-克里斯·C.

P.S.
将sql语句更新为



So, yes, in the query I do want to return the distinct Divisions (within the given agency).
Why am I doing this? Because I don''t control the input data, so the division names (and agencies/department/group/section/branch) can change at any point in time. By doing this I hope that anyone consuming the service will be able to debug why they can no longer get a list of employees in their agency/division without calling me (whether they will or not is a different story).

Does this help? I guess I simplified it too much (looks like a mistake in the question)

Thanks for the answers so far, I''m starting to work through them and will mark which ones work.
-Chris C.

P.S.
Updated sql statement to

WHERE   Agency= @Agency

(原为:Division = @ Agency)

(was: division=@Agency)

推荐答案

尝试一下,
Try this,
string Agency = "";
var query = (from c in context.tEmp
             where c.Agency.Equals(Agency)
             select c.Division).Distinct();


这等效于


This is equivalent to

SELECT DISTINCT division
FROM tEmp
WHERE division= @Agency



但是query语句仅在使用时执行,例如,



But query statement would execute only when you use it, for example,

query.Count() // when you call this statement then the query gets executed.


context.tEmp.Where(p =&>; p.Agency ==代理商).Distinct();这是我会做的,假设第一位是正确的(我从不使用您正在使用的语法).

当然,代理商需要有一个值,可以进行搜索,您会得到完整的实体列表,而不是只有一个属性的匿名类.
context.tEmp.Where(p => p.Agency == Agency).Distinct(); is how I''d do it, assuming the first bit is right ( I never use the syntax you''re using ).

Of course, Agency needs to have a value, to search and you''re getting a full entity list, not an anonymous class with just the one property.


您需要做的是创建一个名称列表,即字符串.这给您带来了独特的东西可以工作的东西.如果对包含名称的某个对象执行不同操作,那么除非重载.
,否则您将获得所有内容.

var divisionNames = divisions.Select(d => d.Name).Distinct();

您可以在 http://http://social.msdn.microsoft.com/forums/en-US/上了解有关使用股票比较器的信息. //social.msdn.microsoft.com/forums/zh-CN/linqprojectgeneral/thread/87adac06-c590-4b6f-93a0-f352d397eb01/ [
What you need to do is create a list of the names, which are strings. That gives you something to have that the distinct will work on. If you do a distinct on some object that contains the name, then you will get everything unless you overload the .

var divisionNames = divisions.Select(d => d.Name).Distinct();

You can read about using an Equity comparer at http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/87adac06-c590-4b6f-93a0-f352d397eb01/[^]

Here is a little code you can run to test what I am saying in debugging:

public class test
{
    public static void run()
    {
        var testgroup1 = new[]
                        {
                                new Class1() {Value = "a"},
                                new Class1() {Value = "a"},
                                new Class1() {Value = "b"},
                        };
        var result1 = testgroup1.Distinct();
        var result3 = testgroup1.Select(i => i.Value).Distinct();
    }
}

public class Class1
{
    public string Value { get; set; }
    public override int GetHashCode()
    {
        return Value.GetHashCode();
    }
}



另一个选择是使用GroupBy,但这要复杂一些.



Another option is using the GroupBy, but that is a bit more complex.


这篇关于我只需要独特的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆