使用LinQ迭代列表以获得所需的结果

时间:2014-07-22 12:43:08

标签: c# linq list

 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中有数据用于过滤。 需要过滤List sourceList

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

你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

您的itemDetails列表似乎与值无关,根据您的示例,结果为“ActivityType5”和“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();

答案 1 :(得分:0)

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

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")));