有没有办法在 LINQ 查询语法中使用 Distinct?

发布于 2024-11-02 11:26:06 字数 259 浏览 1 评论 0原文

有没有办法重写:

var tbl = ds.TABLES;
var q = from c in tbl
        select c.TABLE_TYPE;
string s = "";
foreach (var item in q.Distinct())
{
    s += "[" + item + "]";
}        
MessageBox.Show(s);

以便 Distinct() 调用位于 LINQ 查询中?

Is there way to rewrite:

var tbl = ds.TABLES;
var q = from c in tbl
        select c.TABLE_TYPE;
string s = "";
foreach (var item in q.Distinct())
{
    s += "[" + item + "]";
}        
MessageBox.Show(s);

So that the Distinct() call is in the LINQ query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

饮湿 2024-11-09 11:26:06

语言集成查询语法中没有 Distinct() 方法语法。您可以做的最接近的事情就是移动当前呼叫:

var q = (from c in tbl
         select c.TABLE_TYPE).Distinct();

There is no Distinct() method syntax in the language integrated query syntax. The closest you could do would be to move the current call:

var q = (from c in tbl
         select c.TABLE_TYPE).Distinct();
一杯敬自由 2024-11-09 11:26:06

LINQ 中的 Distinct 扩展方法没有等效的查询语法。

请参阅 https:// learn.microsoft.com/en-us/archive/blogs/charlie/linq-farm-using-distinct-and-avoiding-lambdas 了解有关原因的更多信息。

The Distinct extension method in LINQ does not have a query syntax equivalent.

See https://learn.microsoft.com/en-us/archive/blogs/charlie/linq-farm-using-distinct-and-avoiding-lambdas for additional information as to why.

ㄟ。诗瑗 2024-11-09 11:26:06
(from c in tbl select c.TABLE_TYPE).Distinct();
(from c in tbl select c.TABLE_TYPE).Distinct();
把昨日还给我 2024-11-09 11:26:06

如果您在选择后放置不同的值,VB 就有此功能。

VB has this functionality if you place the distinct after select.

星光不落少年眉 2024-11-09 11:26:06

您可以捕获 HashSet 并将 where 子句放在 select 之前:

var hs = new HashSet<char>();

from c in "abcabcd"
where hs.Add(c)
select c;

You may capture HashSet and put where clause before select:

var hs = new HashSet<char>();

from c in "abcabcd"
where hs.Add(c)
select c;
玩心态 2024-11-09 11:26:06

在为 LINQ 寻找 Distinct 函数时,发现这个问题并意识到它不存在,我的解决方法是使用 GroupBy()。
明显的问题是,distinct-set 不包含所有数据(假设您有三个字段,但只想在两个字段上进行区分,而缺少最后一个字段的值,但是,话又说回来,t-sql 中的 DISTINCT工作方式相同)。

LINQ 代码(因此转储):

void Main()
{
    var gt = new GenerateThings();
    var dlist = gt.list();
    dlist.Dump();

    dlist.GroupBy(x => new {x.id, x.cat}).Dump();
}

public class model
{
    public int id {get;set;}
    public int cat {get;set;}
    public int type {get;set;}
}

public class GenerateThings
{
    public List<model>list()
    {
        var dlist = new List<model>();
        dlist.Add(createNew(1, 1, 1));
        dlist.Add(createNew(1, 1, 1));
        dlist.Add(createNew(1, 2, 1));
        dlist.Add(createNew(1, 2, 1));
        dlist.Add(createNew(1, 1, 2));
        dlist.Add(createNew(1, 1, 2));
        dlist.Add(createNew(1, 1, 2));
        return dlist;
    }
    private model createNew(int id, int cat, int type){
        return new model{
            id = id,
            cat = cat,
            type = type
        };
    }
}

LINQ 转储结果

In the search for a Distinct-function for LINQ upon finding this question and realizing it doesn't exist, my workaround is by using GroupBy().
The obvious problem is that the distinct-set doesn't contain all the data (say you have three fields but only want to distinct on two fields missing out on the value for the last field, but, then again, DISTINCT in t-sql works the same way).

LINQ-code (hence the Dump):

void Main()
{
    var gt = new GenerateThings();
    var dlist = gt.list();
    dlist.Dump();

    dlist.GroupBy(x => new {x.id, x.cat}).Dump();
}

public class model
{
    public int id {get;set;}
    public int cat {get;set;}
    public int type {get;set;}
}

public class GenerateThings
{
    public List<model>list()
    {
        var dlist = new List<model>();
        dlist.Add(createNew(1, 1, 1));
        dlist.Add(createNew(1, 1, 1));
        dlist.Add(createNew(1, 2, 1));
        dlist.Add(createNew(1, 2, 1));
        dlist.Add(createNew(1, 1, 2));
        dlist.Add(createNew(1, 1, 2));
        dlist.Add(createNew(1, 1, 2));
        return dlist;
    }
    private model createNew(int id, int cat, int type){
        return new model{
            id = id,
            cat = cat,
            type = type
        };
    }
}

LINQ-dump result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文