0

次のシナリオを実装する方法の答えが見つかりません (メソッドを探す方法がわかりません)。C#.NET FW4.5、を SQLに使用し、Linqパターン リポジトリを設計します。

すべてのデバイスを選択する場合は、次のコードを使用します。

/// <summary>
/// Get all Devices
/// </summary>
/// <returns></returns>
public IQueryable<Device> GetDevices()
{
    return myDataContext.Devices;
}

ID でデバイスを選択する場合は、次のコードを使用します。

/// <summary>
/// Get Device by ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Device GetDevice(int id)
{
    return myDataContext.Devices.SingleOrDefault(
        d => d.intDeviceId == id);
}

しかし、次の状況をどのように実装できますか?

いくつかの条件があるすべてのデバイスを選択します(そして戻りIQueryable<Device>ます)

4

2 に答える 2

3

試す:

return myDataContext.Devices.Where(
        d => yourConditions).AsQueryable();
于 2013-02-10T10:44:18.507 に答える
2

あなたはそれを次のように使うことができます:

public IQueryable<Device> GetDeviceUsingPredicate(Expression<Func<Device,bool>> predicate)
{
    return myDataContext.Devices.Where(predicate).AsQueryable();
}

ジェネリックで使用するには:

public IQueryable<T> GetDeviceUsingPredicate(Expression<Func<T,bool>> predicate)
{
    return myDataContext.GetTable<T>().Where(predicate).AsQueryable();
}
于 2013-02-10T10:46:52.547 に答える