Select from table using model properties that is not null

时间:2015-05-08 10:00:57

标签: c# entity-framework linq

I want to select values from table where my model properties that has a value. This is my Model.

<FrameLayout
    android:id="@+id/login_button_login"
    android:background="@drawable/apptheme_btn_default_holo_dark"
    android:layout_width="280dp"
    android:layout_gravity="center"
    android:layout_height="40dp">
    <Button
        android:clickable="false"
        android:drawablePadding="15dp"
        android:layout_gravity="center"
        style="@style/WhiteText.Small.Bold"
        android:drawableLeft="@drawable/lock"
        android:background="@color/transparent"
        android:text="LOGIN" />
</FrameLayout>

I want to select from table where values are equal to my model property that is filled up.

For Example:

   $('#cssmenu a').on('click', function(e){
        e.preventDefault();
        $('#cssmenu ul ul').hide();
        $(this).next().show();
    });

How to do this?

2 个答案:

答案 0 :(得分:0)

假设Books中的属性与Library中的属性具有相同的名称,您可能正在寻找类似的内容:

var b = _bookService.GetBooks();

foreach (PropertyInfo p in typeof(Library).GetProperties())
{
    // skip "books" property
    if (p.Name == "books")
    {
        continue;
    }

    // skip null values from lib
    if (p.GetValue(lib) == null)
    {
        continue;
    }

    // build condition dynamically
    var value = Expression.Constant(p.GetValue(lib));
    var param = Expression.Parameter(typeof (Books));
    var predicate = Expression.Lambda<Func<Books, bool>>(
        Expression.Equal(
            Expression.Property(param, p.Name), 
            value
        ),
        param
    );

    b = b.Where(predicate);
}

b必须是可查询的,如果它不是第一行更改为var b = _bookService.GetBooks().AsQueryable();

答案 1 :(得分:0)

它将是:

public ActionResult(Library lib)
{
    var b = _bookService.GetBooks();
    lib.books = b.Where(x => x.Color == lib.Color && x.ShelfNumber == lib. ShelfNumber && x.BookName == lib.BookName )
}