我有我的BLL一些方法,从数据库中获取一些记录,并通过它结合数据控件如GridView或...与UI
是否方法我可以选择我的返回数据类型IQueryable<t>或Ilist<t>.
我的问题是哪一个对我更好,为什么?其实我不知道这两种类型之间的区别,我不知道哪种情况最适合哪种情况?
谢谢
我遇到了使用AND运算符使用IQueryable.Where子句进行多重搜索的问题.我知道问题是当一个参数为空或者为null时,它正在搜索带有另一个参数的""并且没有返回结果但是我不想通过扩展和可笑的嵌套if来检查空值或者8个参数的空白.我用谷歌搜索了几天,OR显然有效,但AND条件没有返回任何内容.
albums = albums.Where(a => a.CA_AlbumName.ToUpper().Contains(searchName.ToUpper()) &&
a.CA_AlbumURL.ToUpper().Contains(searchURL.ToUpper()));
Run Code Online (Sandbox Code Playgroud)
如果两个参数都不是空白,则上述方法有效,但如果其中一个参数不是,则返回任 我已经尝试构建基于非null的where,如下所示,但结果相同:
if (!String.IsNullOrEmpty(searchName))
{
albums = albums.Where(a => a.CA_AlbumName.ToUpper().Contains(searchName.ToUpper()));
}
if (!String.IsNullOrEmpty(searchURL))
{
albums = albums.Where(a => a.CA_AlbumURL.ToUpper().Contains(searchURL.ToUpper()));
}
Run Code Online (Sandbox Code Playgroud) //I need to deifne productQuery here
if (test == true)
{
var productQuery = ordersRepository.ProductIn; //it returns IQueryable<ProductIn> type
}
else
{
var productQuery = ordersRepository.ProductOut; //it returns IQueryable<ProductOut> type
}
Run Code Online (Sandbox Code Playgroud)
如何定义productQuery变量?
谢谢!
[编辑]
dynamic productType;
if (test == true)
{
productType = ordersRepository.ProductIn; //it returns IQueryable<ProductIn> type
}
else
{
productType = ordersRepository.ProductOut; //it returns IQueryable<ProductOut> type
}
var productQuery = productType as IQueryable<ProductIn>;
if (productQuery == null)
{
productQuery = productType as IQueryable<ProductIn>;
}
Run Code Online (Sandbox Code Playgroud)
我这样做,是不是正确的方式?
当我尝试在LINQ to Entities中运行以下查询时出现错误:
var query = DBConn.myView
.Select(dm => new App.DTOs.MyDTO
{
ID = dm.ID,
Prop1 = dm.Prop1
....
})
.Where(dm => dm.TypeID != 4);
Run Code Online (Sandbox Code Playgroud)
错误消息是:
LINQ expression node type 'TypeID' is not supported in LINQ to Entities
Run Code Online (Sandbox Code Playgroud)
它在.ToList()通话中失败:
private List<MyDTO> lstDTO;
lstDTO = query.ToList();
Run Code Online (Sandbox Code Playgroud)
但是,如果我.Where()从查询中删除它,它的工作原理.有没有办法在Where这个查询中使用该子句?
我试图使用select子句从数据库查询中挑选一个与指定名称字段匹配的对象,如下所示:
objectQuery = from obj in objectList
where obj.Equals(objectName)
select obj;
Run Code Online (Sandbox Code Playgroud)
在我的查询的结果视图中,我得到:
base {System.SystemException} = {"Boolean Equals(System.Object)"}
在那里我应该期待有点像Car,Make或Model
有人请解释我在这里做错了什么吗?
有问题的方法可以在这里看到:
// this function searches the database's table for a single object that matches the 'Name' property with 'objectName'
public static T Read<T>(string objectName) where T : IEquatable<T>
{
using (ISession session = NHibernateHelper.OpenSession())
{
IQueryable<T> objectList = session.Query<T>(); // pull (query) all the objects from the table in the database
int count = objectList.Count(); // …Run Code Online (Sandbox Code Playgroud) 是否可以向IQueryable添加扩展方法,将其转换为另一种类型的IQueryable?
我在寻找像这样的东西;
IQueryable<foo> source;
IQueryable<bar> result = source.Convert<bar>();
Run Code Online (Sandbox Code Playgroud)
我有这个,显然它不起作用.大声笑
public static IQueryable<T1> Convert<T2>(this IQueryable<T1> source) where T1 : class
{
// Do some stuff
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我试图将C#代码转换为VB代码:
在C#代码中,它可以调用AddOrUpdate方法,但是当我在VB中编码时,它在intellisense中找不到.
c#代码:
protected override void Seed(eManager.Web.Infrastructure.DepartmentDB context){
context.Departments.AddOrUpdate( d => d.Name,
new Department() { Name = "Engineering"},
new Department() { Name = "Sales"}
);
}
Run Code Online (Sandbox Code Playgroud)
VB代码:
但是当我用VB编写方法AddOrUpdate()时,我找不到它.DepartmentDB类的声明是这样的:
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Data.Entity
Imports eManager.Domain
Public Class DepartmentDB
Inherits DbContext
Implements IDepartmentDataSource
Private _DBEmployees As DbSet(Of Employee)
Private _DBDepartments As DbSet(Of Department)
Public ReadOnly Property Departments As IQueryable(Of Department) Implements IDepartmentDataSource.Departments
Get
Return _DBDepartments
End Get
End Property
Public …Run Code Online (Sandbox Code Playgroud) 我有一个对象IQueryable,代表我的db-model上的一些查询.现在我需要对那个IQeryable进行一些验证并以这种方式返回对象
private IQuerable<myTableType> ValidateQery ( IQuerable<myTableType> query , string user )
{
return query.Where ( x => db.tblUsers.Where( y => y.User == user && y.Privilege >= x.PrivilegeRequired).Any() )
}
Run Code Online (Sandbox Code Playgroud)
现在我想了解的是,如果Any()子句是immidatly执行的,或者它是否生成将与之前合并的T-SQL.我问这个因为我不应该在那一刻避免对db执行查询.
编辑:感谢'using'子句的建议,这是我现在做的一个错误(编写这个代码示例),因为我现在无法测试代码(我这时机器上没有可视化工作室)但是我的问题一般是关于那种情况.
使用Linq,很容易执行一个函数来投影IEnumerable.
var orders = new List<Order>();
orders.Where(x => x.Id > 50).Select(x => new SomethingElse(x.Name));
Run Code Online (Sandbox Code Playgroud)
使用EntityFramework和IQueryable,这是不可能的.而是在运行时获得不受支持的异常,因为您在Select中没有函数.
不起作用:
Run Code Online (Sandbox Code Playgroud)var db = new Order(); db.MyEntities.Where(x => x.Id > 50).Select(x => new SomethingElse(x.Name)).Take(10);
是否存在,或者是否有可能为上述工作创造一种方式?
我理解它不起作用,因为它无法将函数转换为SQL可以理解的东西,但是,如果有一个名为'Execute'的扩展方法可以在执行所有linq之前和之后在内存中执行,那会起作用吗?
数据库将仅拉动> 50并且仅在TOP 10之后
Run Code Online (Sandbox Code Playgroud)var db = new Order(); db.MyEntities.Where(x => x.Id > 50).Execute(x => new SomethingElse(x.Name)).Take(10);
澄清:要求是即使在Sleect()之后仍然保持IQueryable
我想在我的ASP .Net Core 3.0 Web API中查询MySql数据库,并有条件地应用一些WHERE筛选器。所以我在我的控制器动作之一中有这个:
[HttpGet]
public async Task<IEnumerable<Customer>> GetCustomers([FromQuery] bool? isActive, [FromQuery] int? typeId, [FromQuery] bool? isProcessed)
{
var customers = _context.Customers.Where(c => c.IsDeleted == false);
if (isActive.HasValue)
customers = customers.Where(c => c.IsActive == isActive.Value);
if (typeId.HasValue)
customers = customers.Where(c => c.TypeId == typeId.Value);
if (isProcessed.HasValue)
customers = customers.Where(c => c.IsProcessed == isProcessed.Value);
return await customers.ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
这完美地工作了,因为在第一行中有一个Where子句:
var customers = _context.Customers.Where(c => c.IsDeleted == false);
Run Code Online (Sandbox Code Playgroud)
但实际上我不想放入Where子句。我只想要这样:
[HttpGet]
public async Task<IEnumerable<Customer>> GetCustomers([FromQuery] bool? isActive, [FromQuery] int? typeId, [FromQuery] …Run Code Online (Sandbox Code Playgroud) iqueryable ×10
c# ×8
linq ×3
asp.net ×2
.net ×1
asp.net-mvc ×1
database ×1
dbset ×1
func ×1
generics ×1
ilist ×1
linq-to-sql ×1
search ×1
select ×1
sql ×1
vb.net ×1
where-clause ×1