Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>public class Account : Base { ... public override Expression&lt;Func&lt;T, bool&gt;&gt; MustNotAlreadyExist() { return (b =&gt; b.Name == name &amp;&amp; b.AccountCode == accountCode).Any(); } ... } </code></pre> <p>The Any() method will return true if any record matches the predicate. It could be argued that it is outside the responsibility of the repository to check for presence of a record before saving. </p> <p><strong>UPDATE:</strong><br> There is a great article on CodeProject that describes a generic Repository for Entity Framework:</p> <p><a href="http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx" rel="nofollow">http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx</a></p> <p>This could be applied to a non-Entity Framework data context. Here is an excerpt that provides a very flexible method for checking for an existing value by accepting the name of a field, a value, and a key value. You can apply this to any Entity type and use it to check for the presence of an entity before attempting a save.</p> <pre><code>/// &lt;summary&gt; /// Check if value of specific field is already exist /// &lt;/summary&gt; /// &lt;typeparam name="E"&gt;&lt;/typeparam&gt; /// &lt;param name="fieldName"&gt;name of the Field&lt;/param&gt; /// &lt;param name="fieldValue"&gt;Field value&lt;/param&gt; /// &lt;param name="key"&gt;Primary key value&lt;/param&gt; /// &lt;returns&gt;True or False&lt;/returns&gt; public bool TrySameValueExist(string fieldName, object fieldValue, string key) { // First we define the parameter that we are going to use the clause. var xParam = Expression.Parameter(typeof(E), typeof(E).Name); MemberExpression leftExprFieldCheck = MemberExpression.Property(xParam, fieldName); Expression rightExprFieldCheck = Expression.Constant(fieldValue); BinaryExpression binaryExprFieldCheck = MemberExpression.Equal(leftExprFieldCheck, rightExprFieldCheck); MemberExpression leftExprKeyCheck = MemberExpression.Property(xParam, this._KeyProperty); Expression rightExprKeyCheck = Expression.Constant(key); BinaryExpression binaryExprKeyCheck = MemberExpression.NotEqual(leftExprKeyCheck, rightExprKeyCheck); BinaryExpression finalBinaryExpr = Expression.And(binaryExprFieldCheck, binaryExprKeyCheck); //Create Lambda Expression for the selection Expression&lt;Func&lt;E, bool&gt;&gt; lambdaExpr = Expression.Lambda&lt;Func&lt;E, bool&gt;&gt;(finalBinaryExpr, new ParameterExpression[] { xParam }); //Searching .... return ((IRepository&lt;E, C&gt;)this).TryEntity(new Specification&lt;E&gt;(lambdaExpr)); } /// &lt;summary&gt; /// Check if Entities exist with Condition /// &lt;/summary&gt; /// &lt;param name="selectExpression"&gt;Selection Condition&lt;/param&gt; /// &lt;returns&gt;True or False&lt;/returns&gt; public bool TryEntity(ISpecification&lt;E&gt; selectSpec) { return _ctx.CreateQuery&lt;E&gt;("[" + typeof(E).Name + "]").Any&lt;E&gt; (selectSpec.EvalPredicate); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload