我只需要独特的结果

我只需要独特的结果

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

问题描述

限时删除!!

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

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)

推荐答案

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.



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.


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

1403页,肝出来的..

09-06 08:06