C# Linq to SQL-如何使用条件进行选择并返回IQueryable
我找不到如何实现以下场景的答案(我不知道寻找什么方法)。我使用C# Linq to SQL-如何使用条件进行选择并返回IQueryable,c#,linq-to-sql,repository-pattern,C#,Linq To Sql,Repository Pattern,我找不到如何实现以下场景的答案(我不知道寻找什么方法)。我使用C#,.netfw4.5,Linq来创建SQL和设计模式存储库 如果要选择所有设备,请使用以下代码: /// <summary> /// Get all Devices /// </summary> /// <returns></returns> public IQueryable<Device> GetDevices() { return myDataContext
C#
,.netfw4.5
,Linq
来创建SQL和设计模式存储库
如果要选择所有设备,请使用以下代码:
/// <summary>
/// Get all Devices
/// </summary>
/// <returns></returns>
public IQueryable<Device> GetDevices()
{
return myDataContext.Devices;
}
/// <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 GetDevices()
{
返回myDataContext.Devices;
}
如果要按ID选择设备,请使用以下代码:
/// <summary>
/// Get all Devices
/// </summary>
/// <returns></returns>
public IQueryable<Device> GetDevices()
{
return myDataContext.Devices;
}
/// <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);
}
//
///按ID获取设备
///
///
///
公用设备GetDevice(内部id)
{
返回myDataContext.Devices.SingleOrDefault(
d=>d.intDeviceId==id);
}
但我如何实现以下情况
选择所有设备,其中有一些条件(并返回IQueryable
)尝试:
return myDataContext.Devices.Where(
d => yourConditions).AsQueryable();
尝试:
您可以像这样使用它:
public IQueryable<Device> GetDeviceUsingPredicate(Expression<Func<Device,bool>> predicate)
{
return myDataContext.Devices.Where(predicate).AsQueryable();
}
public IQueryable GetDeviceUsingPredicate(表达式谓词)
{
返回myDataContext.Devices.Where(谓词).AsQueryable();
}
要与泛型一起使用:
public IQueryable<T> GetDeviceUsingPredicate(Expression<Func<T,bool>> predicate)
{
return myDataContext.GetTable<T>().Where(predicate).AsQueryable();
}
public IQueryable GetDeviceUsingPredicate(表达式谓词)
{
返回myDataContext.GetTable().Where(谓词).AsQueryable();
}
您可以像这样使用它:
public IQueryable<Device> GetDeviceUsingPredicate(Expression<Func<Device,bool>> predicate)
{
return myDataContext.Devices.Where(predicate).AsQueryable();
}
public IQueryable GetDeviceUsingPredicate(表达式谓词)
{
返回myDataContext.Devices.Where(谓词).AsQueryable();
}
要与泛型一起使用:
public IQueryable<T> GetDeviceUsingPredicate(Expression<Func<T,bool>> predicate)
{
return myDataContext.GetTable<T>().Where(predicate).AsQueryable();
}
public IQueryable GetDeviceUsingPredicate(表达式谓词)
{
返回myDataContext.GetTable().Where(谓词).AsQueryable();
}
这真的很容易,现在我不明白,我怎么找不到这个方法。谢谢。这真的很容易,现在我不明白,我怎么也找不到这个方法。谢谢。你好,谢谢你的回复。如果我比较你和X.L.Ant的答案,有什么更好的方法?我所看到的样本中,reposity模式得到了N mehod,它每返回一个其他的东西。你的例子是通用的。在将来的代码维护方面,哪种方法更好?您可以使用泛型为linq到sql实体创建一个泛型存储库,如更新的答案所示。在维护方面,我更喜欢通用的方式。这取决于您的应用要求。感谢您的解释。只有两个注释(如果有人应该实现这一点):1)必须导入命名空间System.Linq.Expressions 2)示例编码:List listDevices=this.MyDevicePository.GetDeviceUsingPredicate(d=>d.bitIsRemoved==false)。ToList();对你是对的。但是对于泛型的使用,您必须使您的存储库成为泛型的,并使用您想要使用的实体构建您的泛型存储库。比如:var deviceRepos=new MyGenericRepository()
然后deviceRepos.GetUsingPredicate(d=>d.bitsremoved==false)代码>或没有泛型,您的示例用法是正确的。您好,谢谢您的回复。如果我比较你和X.L.Ant的答案,有什么更好的方法?我所看到的样本中,reposity模式得到了N mehod,它每返回一个其他的东西。你的例子是通用的。在将来的代码维护方面,哪种方法更好?您可以使用泛型为linq到sql实体创建一个泛型存储库,如更新的答案所示。在维护方面,我更喜欢通用的方式。这取决于您的应用要求。感谢您的解释。只有两个注释(如果有人应该实现这一点):1)必须导入命名空间System.Linq.Expressions 2)示例编码:List listDevices=this.MyDevicePository.GetDeviceUsingPredicate(d=>d.bitIsRemoved==false)。ToList();对你是对的。但是对于泛型的使用,您必须使您的存储库成为泛型的,并使用您想要使用的实体构建您的泛型存储库。比如:var deviceRepos=new MyGenericRepository()
然后deviceRepos.GetUsingPredicate(d=>d.bitsremoved==false)代码>或没有泛型,您的示例用法是正确的。