列表中存在列表的条件

时间:2017-02-06 16:27:15

标签: c# json linq api model-view-controller

我正在编写一个以下列格式返回JSON的API方法:

{  
   "Results":[  
      {  
         "Rooms":[  
            {  
               "Name":"Deluxe Room",
               "Adults":2,
               "Children":0
            },
            {  
               "Name":"Single Room",
               "Adults":1,
               "Children":0
            }
         ],
         "Options":[  
            {  
               "Name":"Room Only",
               "Price":"100" 
            },
            {  
               "Name":"Breakfast",
               "Price":"200" 
            }
         ]
      }
   ]
}

我从数据中形成这个JSON,我得到Rooms的组合:每个2个房间一起只有1个选项。

我想查看,如果房间组合(姓名/成人/儿童)已经可用,我只想将option添加到现有的Options

类别:

public class Results
{
    public List<RoomList> Rooms { get; set; }
    public List<Option> Options { get; set; }
}

所以我要做的是:遍历Results,并在每个Result内部,我想检查RoomList是否已在另一个Result中可用:如果它可用,我将Option添加到列表:Options。如果是新的,我会向Results添加新项目。

我试过这样做:

//Fill roomOption

//Fill roomList

                            Results Results= new Results();
                            if (!Results.Exists(e => e.RoomList == roomList))
                            {
                               Results result = new Results()
                                {
                                    RoomList = roomList
                                };
                                //add the option
                                if (result.Options == null)
                                    result.Options = new List<Option>();
                                result.Options.Add(roomOption);
                                results.Add(result);
                            }
                            else 
                            {
                                result= Results.Where(e => e.RoomList == roomList).FirstOrDefault();
                                //Add the option to the already existing room list
                                if (result.Options == null)
                                    result.Options = new List<Option>();
                                result.Options.Add(roomOption);

                            }

我需要用其他东西替换条件Results.Exists(e => e.RoomList == roomList)

任何人都可以提供帮助

2 个答案:

答案 0 :(得分:1)

假设结果是IEnumerable of Result

选项#1

首先,您需要像Room

那样覆盖Room对象上的equals
public override bool Equals(object obj)
{
    var other = (Room) obj;
    return string.Equals(Name, other.Name) 
        && Adults == other.Adults 
        && Children == other.Children;
}

public override int GetHashCode()
{
    unchecked
    {
        var hashCode = Name.GetHashCode();
        hashCode = (hashCode * 397) ^ Adults;
        hashCode = (hashCode * 397) ^ Children;
        return hashCode;
    }
}

然后在您的代码中,您可以非常优雅地放置

if (!Results.Any(e => e.Rooms.Count() == roomList.Count() 
     && e.Rooms.All(x => roomList.Contains(x))))

并调整“result = Results.Where(e =&gt; e.RoomList == roomList).FirstOrDefault()”to

result = Results.FirstOrDefault(e => e.Rooms.Count() == roomList.Count()
    && e.Rooms.All(x => roomList.Contains(x)));

旁注如果您关注空值或将房间与其他类型的对象进行比较,请使用此等于方法

public override bool Equals(object obj)
{
    if (ReferenceEquals(null, obj)) return false;
    if (ReferenceEquals(this, obj)) return true;
    if (obj.GetType() != this.GetType()) return false;
    return Equals((Room) obj);
}

protected bool Equals(Room other)
{
    return string.Equals(Name, other.Name) && Adults == other.Adults && Children == other.Children;
    }

    public override int GetHashCode()
    {
        unchecked
        {
            var hashCode = Name.GetHashCode();
            hashCode = (hashCode * 397) ^ Adults;
            hashCode = (hashCode * 397) ^ Children;
            return hashCode;
        }
    }

选项2

如果您不想或不能覆盖等于那么您可以制作这样的方法

private bool DoesRoomMatch(Room rm1, Room rm2)
{
    return string.Equals(rm1.Name, rm2.Name) 
        && rm1.Adults == rm2.Adults 
        && rm1.Children == rm2.Children;
}

然后这两个陈述变为

if (!Results.Any(e => e.Rooms.Count() == roomList.Count() 
     && e.Rooms.All(x => roomList.Any(y => DoesRoomMatch(x, y)))))

result = Results.FirstOrDefault(e => e.Rooms.Count() == roomList.Count()
    && e.Rooms.All(x => roomList.Any(y => DoesRoomMatch(x, y))));

BTW您可以使用FirstOrDefault代替Where

答案 1 :(得分:0)

据我了解,您必须检查结果中是否已有房间。

所以你应该遍历roomList,并检查每个元素是否为Results.Rooms.Contains(room)。

如果需要,请覆盖Equals方法.. https://msdn.microsoft.com/en-us/library/bhkz42b3(v=vs.110).aspx

希望它有所帮助!