Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用linq在表中查找部分重复项_C#_Linq - Fatal编程技术网

C# 使用linq在表中查找部分重复项

C# 使用linq在表中查找部分重复项,c#,linq,C#,Linq,我想在我的行程表中找到部分重复记录,如下所示: 端口TripIdStatusId 475210 50001313 47521150001313 47521250001313 47521350001213 47521450001213 47521550004513 47521650004513 47521750004513 47521850004513 47521850004512 您可以组合使用分组、where条件并选择所需的值: YourList .GroupBy(c => new

我想在我的行程表中找到部分重复记录,如下所示:


端口TripIdStatusId
475210 50001313
47521150001313
47521250001313
47521350001213
47521450001213
47521550004513
47521650004513
47521750004513
47521850004513
47521850004512

您可以组合使用分组、where条件并选择所需的值:

YourList
.GroupBy(c => new { c.TripId, c.Port })
.Where(grp => grp.Count() > 1)
.Select(grp => new { Port = grp.Key.Port, TripId = grp.Key.TripId });
完整工作示例:

public class Trip
{
    public string Port { get; set; }
    public string TripId { get; set; }
    public string StatusId { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        IList<Trip> tripList = new List<Trip>();

        tripList.Add(new Trip() { TripId = "123", Port = "1" });
        tripList.Add(new Trip() { TripId = "124", Port = "2" });
        tripList.Add(new Trip() { TripId = "124", Port = "3" });
        tripList.Add(new Trip() { TripId = "126", Port = "4" });
        tripList.Add(new Trip() { TripId = "126", Port = "4" });

        var doubleTrip = tripList
            .GroupBy(c => new { c.TripId, c.Port })
            .Where(grp => grp.Count() > 1)
            .Select(grp => new { Port = grp.Key.Port, TripId = grp.Key.TripId });

        foreach (var d in doubleTrip)
        {
            Console.WriteLine("TripId: {0}, Port: {1}", d.TripId, d.Port);
        }

        Console.ReadLine();
    }
公务舱旅行
{
公共字符串端口{get;set;}
公共字符串TripId{get;set;}
公共字符串StatusId{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
IList tripList=新列表();
添加(newtrip(){TripId=“123”,Port=“1”});
添加(新Trip(){TripId=“124”,Port=“2”});
添加(新Trip(){TripId=“124”,Port=“3”});
添加(新Trip(){TripId=“126”,Port=“4”});
添加(新Trip(){TripId=“126”,Port=“4”});
var doubleTrip=三重列表
.GroupBy(c=>new{c.TripId,c.Port})
.Where(grp=>grp.Count()>1)
.Select(grp=>new{Port=grp.Key.Port,TripId=grp.Key.TripId});
foreach(双重跳闸中的var d)
{
WriteLine(“TripId:{0},端口:{1}”,d.TripId,d.Port);
}
Console.ReadLine();
}

您可以使用
IEqualityComparer
。您的类实现可能如下所示

class DistinctItemComparer : IEqualityComparer<Trip> {

public bool Equals(Item x, Item y) {
    return x.Port == y.Port &&
        x.TripId== y.TripId;
}

public int GetHashCode(Trip obj) {
    return obj.Port.GetHashCode() ^
        obj.TripId.GetHashCode();
}

html表格只是用来格式化我的数据以将其显示为表格。:-)相同端口相同tripId(475218/500045)相同端口和tripId或相同端口或tripId?相同端口和tripId这很好,正是我想要的。很好:)为您添加了一个示例!
var distinctRecords = YourList.Distinct(new DistinctItemComparer());