标签: iqueryable

IQueryable <t>或IList <t>

我有我的BLL一些方法,从数据库中获取一些记录,并通过它结合数据控件如GridView或...与UI
是否方法我可以选择我的返回数据类型IQueryable<t>Ilist<t>.
我的问题是哪一个对我更好,为什么?其实我不知道这两种类型之间的区别,我不知道哪种情况最适合哪种情况?

谢谢

asp.net ilist iqueryable linq-to-sql

0
推荐指数
1
解决办法
299
查看次数

IQueryable.Where子句并且不使用多个搜索参数

我遇到了使用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)

asp.net-mvc search iqueryable where-clause

0
推荐指数
1
解决办法
6028
查看次数

C#不同实体类型的IQueryable定义

//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)

我这样做,是不是正确的方式?

.net c# asp.net generics iqueryable

0
推荐指数
1
解决办法
955
查看次数

LINQ to Entities中不支持LINQ表达式节点类型

当我尝试在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这个查询中使用该子句?

c# linq linq-to-entities entity-framework iqueryable

0
推荐指数
1
解决办法
1173
查看次数

C#Select子句返回系统异常而不是相关对象

我试图使用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,MakeModel

有人请解释我在这里做错了什么吗?


有问题的方法可以在这里看到:

// 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)

c# sql database select iqueryable

0
推荐指数
1
解决办法
660
查看次数

IQueryable <T1>扩展方法,转换为IQueryable <T2>

是否可以向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# iqueryable generic-collections

0
推荐指数
1
解决办法
251
查看次数

使用VB.NET找不到AddOrUpdate()方法?

我试图将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)

c# vb.net iqueryable

0
推荐指数
1
解决办法
701
查看次数

对IQueryable对象执行Any()子句

我有一个对象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'子句的建议,这是我现在做的一个错误(编写这个代码示例),因为我现在无法测试代码(我这时机器上没有可视化工作室)但是我的问题一般是关于那种情况.

c# linq iqueryable

0
推荐指数
1
解决办法
111
查看次数

在IQueryable中执行函数

使用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中没有函数.

不起作用:

var db = new Order();
db.MyEntities.Where(x => x.Id > 50).Select(x => new SomethingElse(x.Name)).Take(10);
Run Code Online (Sandbox Code Playgroud)

是否存在,或者是否有可能为上述工作创造一种方式?

我理解它不起作用,因为它无法将函数转换为SQL可以理解的东西,但是,如果有一个名为'Execute'的扩展方法可以在执行所有linq之前和之后在内存中执行,那会起作用吗?

数据库将仅拉动> 50并且仅在TOP 10之后

var db = new Order();
db.MyEntities.Where(x => x.Id > 50).Execute(x => new SomethingElse(x.Name)).Take(10);
Run Code Online (Sandbox Code Playgroud)

澄清:要求是即使在Sleect()之后仍然保持IQueryable

c# entity-framework iqueryable func

0
推荐指数
1
解决办法
3121
查看次数

使用EF Core和条件WHERE子句从数据库读取行时出现问题

我想在我的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)

c# linq iqueryable dbset

0
推荐指数
1
解决办法
37
查看次数