C#中列表选择的谓词问题

时间:2017-07-07 10:28:41

标签: c# .net lambda predicate

我有一个curried的typed-Lambda表达式,我希望它能执行选择并派生出一个独特的元素。可悲的是,有一个运行时例外。

以下是代码:

Entities relation = queryType(
            "test-instances",
            (new KeyValuePair<String, String>("cycle-id", testSetID))
        );

Entity target = (relation.entities.ToList<Entity>()).Find(
            //select entity with field 'name'
            e => (e.fields.Find(
                //and with non-epsillon value
                f => !(f.values.Find(
                    //of value 'targetValue'
                    v => (v.value.Equals(targetValue))
                ).value.Equals(""))
            )).name.Equals("name")
        );

queryType()的定义:

       private Entities queryType(String entitiesType, params KeyValuePair<String, String>[] values)
    {
        addStandardHeaders();
        String query = parsePredicate(values);
        Task<HttpResponseMessage> filterTask = client.GetAsync(client.BaseAddress + RESTapi.ALMentitiesQuery(domain, project, entitiesType, query));
        Task.WaitAny(filterTask);
        //Callback:
        HttpResponseMessage result = filterTask.Result;
        if (result.IsSuccessStatusCode)
        {
            updateCookies(client.BaseAddress + RESTapi.ALMentitiesQuery(domain, project, entitiesType, query));
            mainHeaders.Clear();
            Task<Stream> output = result.Content.ReadAsStreamAsync();
            Task.WaitAny(output);
            //Callback:
            return (Entities)fromJSON(output.Result, typeof(Entities)); 
        }
        else
        {
            Exception exception = getException(result);
            throw new HttpException(exception.Title);
            return null;
        }
    }

函数parsePredicate()接受pairs()并将它们编码为服务器理解的语言的谓词。

例外是在最内层的功能;绑定变量v应用为nullef可以使用,我使用的数据模型也很合理。

有什么想法吗?

以下是Entities的课程:

public class Entities
{
    public List<Entity> entities{get;set;}

    public long totalResults{get;set;}

    public Entities(){entities = new List<Entity>();}
}


public class Entity
{
    public List<Field> fields { get; set; }

    public Entity() { fields = new List<Field>(); }
}

public class Field
{
    public String name { get; set; }


    public List<Value> values { get; set; }

    public Field() { values = new List<Value>(); }
}


public class Value
{
    public String value{get;set;}
}

0 个答案:

没有答案