使用动态Expression <func <t,bool =“” >>

时间:2018-10-09 14:49:06

标签: c# entity-framework

我有此代码

  public class PropertyFilterSpecification : BaseSpecification<Property>
{
    public PropertyFilterSpecification(PropertySearchCriteria sc)

    : base(i => 
    (!sc.VendorId.HasValue || i.VendorId == sc.VendorId)
    &&
    (string.IsNullOrWhiteSpace(sc.Name) || i.Name.Contains(sc.Name))
    &&
    (string.IsNullOrWhiteSpace(sc.Name) || i.Name.Contains(sc.Name))
    )
    {
        AddInclude(b => b.Vendor);
        AddInclude($"{nameof(Order.OrderItems)}.{nameof(OrderItem.OrderedProperty)}");
    }
}

现在我需要使用动态表达式>

如果

,我需要我的代码添加条件
        : base(i => 
        addif (!sc.VendorId.HasValue , i.VendorId == sc.VendorId)
   .
        .addif (string.IsNullOrWhiteSpace(sc.Name) || i.Name.Contains(sc.Name))
      .`enter code here`addif 
        (string.IsNullOrWhiteSpace(sc.Name) || i.Name.Contains(sc.Name))
        )

并请如何构建动态creitirea

1 个答案:

答案 0 :(得分:-1)

namespace App\Models\General;

use App\Models\System\Upload;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Session
 * package App.
 */
class Event extends Model
{
    protected $fillable = ['name', 'description', 'start_time', 'end_time', 'is_published'];

    public function uploads()
    {
        return $this->morphMany(Upload::class, 'uploadable');
    }
}