简体   繁体   English

如何使用实体框架和 linq 仅获取具有唯一属性的记录?

[英]How can I get only records with a unique property using entity framework and linq?

public class Person
{
     public string firstName;
     public string lastName;
}

I want a list of all Persons with a unique first name.我想要一个具有唯一名字的所有人员的列表。

Persons table人员表

Tom Haverford
Tom Baker
Amy Pond
Amy Santiago
Trish Walker
Chidi Anagonye

The query should return查询应该返回

Trish, Chidi

I've tried using Distinct and a combination of GroupBy and Select, but those return Trish, Chidi, Tom, Amy .我试过使用 Distinct 以及 GroupBy 和 Select 的组合,但那些返回Trish, Chidi, Tom, Amy

Demo on dotnet fiddle dotnet fiddle 上的演示

You can Group by then count number of duplicated items.您可以Group by然后count重复项目的数量。 After that, you can get the item with count value equals to 1 like below.之后,您可以获得count数值等于 1 的项目,如下所示。

    var arr = new []
    {
        new Person {    firstName = "Tom", lastName = "Haverford" },
        new Person {    firstName = "Tom", lastName = "Baker"},
        new Person {    firstName = "Amy", lastName = "Pond" },
        new Person {    firstName = "Amy", lastName = "Santiago"},
        new Person {    firstName = "Trish", lastName = "Walker"},
        new Person {    firstName = "Chidi", lastName ="Anagonye" }
    };

    var result = arr.GroupBy(p => p.firstName).Select(g => new { Name = g.Key, Count = g.Count()});
    foreach(var item in result.Where(p => p.Count == 1))
      Console.WriteLine(item.Name); 

Output输出

Trish
Chidi

You can use group by and count functionality together for this :您可以为此一起使用 group by 和 count 功能:

1. Get a list of all persons from DB : 1.从DB获取所有人员的列表:

var personList = (from p in db.Person select p).ToList(); // I assumed here that your db obj name is 'db' and table name is 'Person'

2. Now apply group by query to get the count of first names : 2. 现在按查询应用分组以获取名字的计数:

var q = from x in personList
    group x by x.firstName into g
    let count = g.Count()
    select new {Count = count, Name = g.First().firstName }; 

3. Now you can get your final result like this : 3. 现在你可以得到这样的最终结果:

var finalResult = (from p in q where p.Count == 1 select p).ToList();

Happy Coding...快乐编码...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Linq获取唯一的实体集? - How can I grab a unique entity set using Linq? 我如何链接两个类,使用Entity Framework和LINQ选择并仍然获得列表输出? - How can I link two classes, select and still get a list output using Entity Framework and LINQ? 如何使用LINQ和Entity Framework对总计进行分组? - How can I do a group by with totals using LINQ and Entity Framework? 如何使用 ExecuteSqlCommand 删除实体框架中的记录? - How can I delete records in Entity Framework using ExecuteSqlCommand? 如何在实体框架中使用linq查询获取特定时间之间的记录 - how to get records between particular time using linq query in entity framework 如何使用 Entity Framework Core 的 Linq 更新表中的多个记录 - How to update multiple records in a table Using the Linq for Entity Framework Core 如何使用 EF 和 linq 获取包含嵌套信息的实体 - How can I get this entity with nested information using EF and linq 当列全为varchar时如何使用LINQ获取唯一记录 - How to get unique records using LINQ when columns are all varchar 在C#和实体框架中,如何确保现有记录无法更改,并且仅在通过检查后才能添加新记录? - In c# and entity framework how can I ensure that existing records cannot be changed and new records can only be added after passing checks? 如何从属性设置器访问实体框架实体的上下文? - How can I get to the context of an Entity Framework entity from a property setter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM