简体   繁体   English

在用户类型列表中查找常见项目

[英]Find common items in list of lists of user type

I have a studyTimeList that contains lists of ScheduleStudyTime - my user type. 我有一个studyTimeList,其中包含ScheduleStudyTime的列表-我的用户类型。 I'm trying to find a common ScheduleStudyTime among the lists. 我正在尝试在列表中找到一个常见的ScheduleStudyTime。 Here is my code: 这是我的代码:

    private class ScheduleStudyTime
    {
        public int STUDTIME_ID { get; set; }

        public int DAY_ID { get; set; }

        public int LESSTIME_ID { get; set; }

        public int SCHOOLYEAR_ID { get; set; }
    }

    private void LoadStudyTime()
    {
        var fourths = dbContext.FOURTH.Where(x => x.CHOOSE_SCHEDULE_FOURTH.Any(a => a.SCHEDVARIANT_ID == ScheduleVariant.SCHEDVARIANT_ID)).ToList();
        int fourthCount = fourths != null ? fourths.Count() : 0;
        List<ScheduleStudyTime>[] studyTimeList = new List<ScheduleStudyTime>[fourthCount];
        for (int i = 0; i <= (fourthCount - 1); ++i)
        {
            int fourthId = fourths[i].FOURTH_ID;
            var chooseStudyTime = from CHOOSE_STUDY_FOURTH in dbContext.CHOOSE_STUDY_FOURTH
                                  where CHOOSE_STUDY_FOURTH.STUDY_TIME.SCHOOLYEAR_ID == Properties.Settings.Default.SchoolYearId &&
                                  CHOOSE_STUDY_FOURTH.FOURTH_ID == fourthId
                                  group CHOOSE_STUDY_FOURTH by new
                                  {
                                      CHOOSE_STUDY_FOURTH.STUDY_TIME.STUDTIME_ID,
                                      CHOOSE_STUDY_FOURTH.STUDY_TIME.DAY_ID,
                                      CHOOSE_STUDY_FOURTH.STUDY_TIME.LESSTIME_ID,
                                      CHOOSE_STUDY_FOURTH.STUDY_TIME.SCHOOLYEAR_ID
                                  }
                                  into gcsf
                                  select new ScheduleStudyTime
                                  {
                                      STUDTIME_ID = gcsf.Key.STUDTIME_ID,
                                      DAY_ID = gcsf.Key.DAY_ID,
                                      LESSTIME_ID = gcsf.Key.LESSTIME_ID,
                                      SCHOOLYEAR_ID = gcsf.Key.SCHOOLYEAR_ID
                                  };
            studyTimeList[i] = chooseStudyTime.ToList();
        }
        var commonStudyTime = studyTimeList.Aggregate((xs, ys) => xs.Intersect(ys).ToList());
    }

How can I do this if commonStudyTime returns zero, even if there are matches 如果commonStudyTime返回零,即使有匹配项,我该怎么做

The Intersect method will use the default comparer which basically checks (for reference types) if the references are the same. Intersect方法将使用默认的比较器,该比较器基本上检查(对于引用类型)引用是否相同。 Since your list has object types, and they are different objects, it returns 0 results. 由于您的列表具有object类型,并且它们是不同的对象,因此它将返回0个结果。

To do what you want, you have to tell the Intersect method how to do the comparison check. 要执行您想要的操作,您必须告诉Intersect方法如何进行比较检查。 So you need something like: 因此,您需要类似:

public class ScheduleStudyTimeComparer : IEqualityComparer<ScheduleStudyTime>
{
    public bool Equals(ScheduleStudyTime x, ScheduleStudyTime y)
    {
        // TODO: Check for nulls and possibly make the decision using
        // other properties as well
        return x.STUDTIME_ID  == y.STUDTIME_ID ;
    }

    public int GetHashCode(ScheduleStudyTime obj)
    {
        return obj.ScheduleStudyTime.GetHashCode();
    }
}

Now tell the Intersect method to use that: 现在告诉Intersect方法使用它:

xs.Intersect(ys, new ScheduleStudyTimeComparer())

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM