DbQuery <tresult>如何实现IEnumerable <tresult>

时间:2017-03-17 12:40:27

标签: c#

我正在检查Assembly EntityFramework.dll,v6.0.0.0,$ EntityFramework.dll $ v4.0.30319 $ NoDynamic \ System.Data.Entity.Infrastructure.DbQuery.cs

的元数据
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace System.Data.Entity.Infrastructure
{
    // Summary:
    //     Represents a non-generic LINQ to Entities query against a DbContext.
    [SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface")]
    [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
    public abstract class DbQuery : IOrderedQueryable, IQueryable, IEnumerable, IListSource, IDbAsyncEnumerable
    {
        // Summary:
        //     The IQueryable element type.
        public virtual Type ElementType { get; }

        // Summary:
        //     Returns a new query where the entities returned will not be cached in the
        //     System.Data.Entity.DbContext.
        //
        // Returns:
        //     A new query with NoTracking applied.
        public virtual DbQuery AsNoTracking();
        //
        // Summary:
        //     Returns a new query that will stream the results instead of buffering.
        //
        // Returns:
        //     A new query with AsStreaming applied.
        [Obsolete("Queries are now streaming by default unless a retrying ExecutionStrategy is used. Calling this method will have no effect.")]
        public virtual DbQuery AsStreaming();
        //
        // Summary:
        //     Returns the equivalent generic System.Data.Entity.Infrastructure.DbQuery<TResult>
        //     object.
        //
        // Type parameters:
        //   TElement:
        //     The type of element for which the query was created.
        //
        // Returns:
        //     The generic set object.
        public DbQuery<TElement> Cast<TElement>();
        //
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override bool Equals(object obj);
        //
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override int GetHashCode();
        //
        [EditorBrowsable(EditorBrowsableState.Never)]
        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public Type GetType();
        //
        // Summary:
        //     Specifies the related objects to include in the query results.
        //
        // Parameters:
        //   path:
        //     The dot-separated list of related objects to return in the query results.
        //
        // Returns:
        //     A new DbQuery<T> with the defined query path.
        //
        // Remarks:
        //     Paths are all-inclusive. For example, if an include call indicates Include("Orders.OrderLines"),
        //     not only will OrderLines be included, but also Orders. When you call the
        //     Include method, the query path is only valid on the returned instance of
        //     the DbQuery<T>. Other instances of DbQuery<T> and the object context itself
        //     are not affected.  Because the Include method returns the query object, you
        //     can call this method multiple times on an DbQuery<T> to specify multiple
        //     paths for the query.
        public virtual DbQuery Include(string path);
        //
        // Summary:
        //     Returns a System.String representation of the underlying query.
        //
        // Returns:
        //     The query string.
        public override string ToString();
    }
}

但我没有找到GetEnumerator()方法。这是怎么发生的?

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

明确实施......来自codeplex

#region IEnumerable

/// <summary>
/// Returns an <see cref="IEnumerator{TElement}" /> which when enumerated will execute the query against the database.
/// </summary>
/// <returns> The query results. </returns>
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
IEnumerator<TResult> IEnumerable<TResult>.GetEnumerator()
{
    ...
}

/// <summary>
/// Returns an <see cref="IEnumerator{TElement}" /> which when enumerated will execute the query against the database.
/// </summary>
/// <returns> The query results. </returns>
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
IEnumerator IEnumerable.GetEnumerator()
{
    ...
}

#endregion

啊......请注意......您正在查看错误的文件... I am checking the metadata of Assembly EntityFramework.dll, v6.0.0.0, $EntityFramework.dll$v4.0.30319$NoDynamic\System.Data.Entity.Infrastructure.**DbQuery.cs**适用于abstract class DbQuery,不适用于class DbQuery<TResult>。作为abstract class,它可能是不完整的(不实现所有接口)。你应该查看 DbQuery`.cs 文件