Making a custom class IQueryable

后端 未结 2 1646
广开言路
广开言路 2021-02-13 22:14

I have been working with the TFS API for VS2010 and had to query FieldCollection which I found isn\'t supported by LINQ so I wanted to create a custom class to make the Field an

2条回答
  •  孤街浪徒
    2021-02-13 23:10

    In your case of wrapping and building a class around an existing IEnumerable Collection type i.e. List,

    you might just use a set of "forward function" wrappers that expose the IQueryable interface:

    public class WorkItemFieldCollection : IEnumerable, IQueryable
    {
        ...
        #region Implementation of IQueryable
    
        public Type ElementType
        {
            get
            {
                return this._fieldList.AsQueryable().ElementType;
            }
        }
    
        public Expression Expression
        {
            get
            {
                return this._fieldList.AsQueryable().Expression;
            }
        }
    
        public IQueryProvider Provider
        {
            get
            {
                return this._fieldList.AsQueryable().Provider;
            }
        }
    
        #endregion
        ...
    }
    

    You can now directly query your WorkItemFieldCollection:

    var workItemFieldCollection = new WorkItemFieldCollection(...);
    var Fuzz = "someStringId";
    var workFieldItem = workItemFieldCollection.FirstOrDefault( c => c.Buzz == Fuzz );
    

提交回复
热议问题