Ejemplo n.º 1
0
        /// <summary>
        /// Returns a CqlQuery which after execution will return the first element of a sequence,
        /// or a default value if the sequence contains no elements.
        /// To execute this CqlQuery use <c>Execute()</c> method.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements of source.</typeparam>
        /// <param name="source">The CqlQuery&lt;TSource&gt; to return the first element of.</param>
        /// <returns><c>a CqlQuery&lt;TSource&gt; which after execution will return default(TSource)</c> if source is empty,
        /// otherwise the first element in source.</returns>
        public static CqlQuerySingleElement <TSource> FirstOrDefault <TSource>(this CqlQuery <TSource> source)
        {
            var ret = new CqlQuerySingleElement <TSource>(source.Table.CreateQuery <TSource>(Expression.Call(
                                                                                                 null, CqlMthHelps.FirstOrDefaultMi,
                                                                                                 new[] { source.Expression, Expression.Constant(1) })).Expression, source);

            source.CopyQueryPropertiesTo(ret);
            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a CqlQuery which after execution will return the first element of the sequence that satisfies a condition
        /// or a default value if no such element is found.
        /// To execute this CqlQuery use <c>Execute()</c> method.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements of source.</typeparam>
        /// <param name="source">The Table&lt;TSource&gt;  to return the first element of.</param>
        /// <param name="predicate">A function to test each element for a condition.</param>
        /// <returns>a CqlQuery&lt;TSource&gt; which after execution will return <c>default(TSource)</c> if source is empty
        /// or if no element passes the test specified by predicate,
        /// otherwise the first element in source that passes the test specified by predicate.</returns>
        public static CqlQuerySingleElement <TSource> FirstOrDefault <TSource>(this Table <TSource> source, Expression <Func <TSource, bool> > predicate)
        {
            var ret = new CqlQuerySingleElement <TSource>(source.Table.CreateQuery <TSource>(Expression.Call(
                                                                                                 null, CqlMthHelps.FirstOrDefault_ForCQLTableMi,
                                                                                                 new[] { source.Expression, Expression.Constant(1), predicate })).Expression, source);

            source.CopyQueryPropertiesTo(ret);
            return(ret);
        }