使用LinQ迭代列表以获得期望的结果

本文关键字:期望 结果 LinQ 迭代 列表 使用 | 更新日期: 2023-09-27 18:14:00

 public class ItemDetails
    {
        public List<string> XAxis { get; set; }
        public List<string> YAxis { get; set; }
        public List<string> Value { get; set; }
    }
    public class Activity
    {
        public string ActivityName { get; set; }
        public string ActivityValue { get; set; }
    }
    public class ActivityQuarterModel
    {
        public Activity Activity { get; set; }
        public Quarter Quarter { get; set; }
    }
    public class Quarter : IEquatable<Quarter>
    {
        public int Year { get; set; }
        public int QuarterIndex { get; set; }

        public Quarter FromDate(DateTime date)
        {
            Quarter quarter = new Quarter()
            {
                Year = date.Year,
                QuarterIndex = 1 + (date.Month - 1) / 3
            };
            return quarter;
        }
        public string GetQuarter(DateTime date)
        {
            string quarter = "Q" + (1 + (date.Month - 1) / 3) + " " + date.Year;
            return quarter;
        }
        public override bool Equals(object otherItem)
        {
            Quarter other = otherItem as Quarter;
            if (other == null) return false;
            return this.Equals(other);
        }
        public bool Equals(Quarter otherItem)
        {
            if (otherItem == null) return false;
            return Year == otherItem.Year && QuarterIndex == otherItem.QuarterIndex;
        }
        public override int GetHashCode()
        {
            unchecked
            {
                int hash = 23;
                hash = (hash * 31) + Year;
                hash = (hash * 31) + QuarterIndex;
                return hash;
            }
        }
    }
    public class ItemComparer : IEqualityComparer<Quarter>
    {
        public bool Equals(Quarter lhs, Quarter rhs)
        {
            if (lhs == null || rhs == null) return false;
            return lhs.Year == rhs.Year && lhs.QuarterIndex == rhs.QuarterIndex;
        }
        public int GetHashCode(Quarter item)
        {
            if (item == null) return 0;
            unchecked
            {
                int hash = 23;
                hash = (hash * 31) + item.Year;
                hash = (hash * 31) + item.QuarterIndex;
                return hash;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ItemDetails itemDetails = new ItemDetails()
            {
                XAxis = new List<string>() { "Q1 2013", "Q2 2013"},
                YAxis = new List<string>() { "Activity1", "Activity2"},
                Value = new List<string>() { "ActivityType3", "ActivityType5" }
            };
            List<ActivityQuarterModel> sourceList = new List<ActivityQuarterModel>()
            {
                new ActivityQuarterModel() { Activity = new Activity(){ ActivityName = "Activity1", ActivityValue = "ActivityType3"}, Quarter = new Quarter() { QuarterIndex = 2, Year = 2013}},
                new ActivityQuarterModel() { Activity = new Activity(){ ActivityName = "Activity1", ActivityValue = "ActivityType5"}, Quarter = new Quarter() { QuarterIndex = 1, Year = 2013}},
                new ActivityQuarterModel() { Activity = new Activity(){ ActivityName = "Activity1", ActivityValue = "ActivityType3"}, Quarter = new Quarter() { QuarterIndex = 3, Year = 2013}},
                new ActivityQuarterModel() { Activity = new Activity(){ ActivityName = "Activity2", ActivityValue = "ActivityType3"}, Quarter = new Quarter() { QuarterIndex = 1, Year = 2013}},
                new ActivityQuarterModel() { Activity = new Activity(){ ActivityName = "Activity1", ActivityValue = "ActivityType5"}, Quarter = new Quarter() { QuarterIndex = 1, Year = 2013}},
            };
        }
    }

我在ItemDetails中有用于过滤的数据。需要过滤列表sourceList

如何检索具有季度"Q1 2013"和活动名称"Activity1"的ActivityValues列表?应该提供ActivityValues。这里是"ActivityType3", "ActivityType5"

你能帮我一下吗?

使用LinQ迭代列表以获得期望的结果

您的itemDetails列表似乎与值无关,并且根据您的示例,结果是"ActivityType5"answers"ActivityType5"

var activityValues = (from aqm in sourceList
                      where (aqm.Activity.ActivityName == "Activity1" &&
                             (aqm.Quarter.QuarterIndex == 1 && aqm.Quarter.Year == 2013)) // Made the assumption that "Q1 2013" is QuarterIndex = 1 and Year = 2013
                      select aqm.Activity.ActivityValue).ToList();

我不确定我是否理解这一点,但这是你在寻找什么?

var results = sourceList.Where(a => a.Quarter.QuarterIndex.Equals(1) 
    && a.Quarter.Year.Equals(2013) 
    && a.Activity.ActivityName.Equals("Activity1") 
    && (a.Activity.ActivityValue.Equals("ActivityType3") || a.Activity.ActivityValue.Equals("ActivityType5")));