刚刚学习LINQ,我在我的测试项目中遇到了一个新手的障碍.你能解释一下我做错了什么吗?
public List<ToDoListInfo> retrieveLists(int UserID)
{
//Integrate userid specification later - need to add listUser table first
IQueryable<ToDoListInfo> lists =
from l in db.ToDoLists
select new ToDoListInfo {
ListID = l.ListID,
ListName = l.ListName,
Order = l.Order,
Completed = l.Completed
};
return lists.ToList<ToDoListInfo>;
}
Run Code Online (Sandbox Code Playgroud)
我收到一条错误说:
Cannont将方法组'ToList'转换为非委托类型'System.Collections.Generic.List'您是否打算调用该方法?
我有以下LINQ代码:
var posts = (from p in db.Posts
.Include("Site")
.Include("PostStatus")
where p.Public == false
orderby p.PublicationTime
select p);
if (!chkShowIgnored.Checked) {
posts = posts.Where(p => p.PostStatus.Id != 90);
}
Run Code Online (Sandbox Code Playgroud)
最后一行(额外的地方)给了我错误:
无法将类型'System.Linq.IQueryable'隐式转换为'System.Linq.IOrderedQueryable'.
我不确定这意味着什么......
为什么我会收到这个错误?
一旦我将"orderby"子句添加到查询中,它之前就编译好了,所以我对所发生的事情有一种预感,但我无法完全理解它.
我做一个sql查询,它返回一个字符串 - 服务名称.这是查询:
IQueryable<string> query = from Comp in ServiceGroupdb.ServiceGroupes
where (Comp.GroupID == groupID)
select Comp.Name;
Run Code Online (Sandbox Code Playgroud)
如何从查询中获取字符串?
假设我需要一个扩展方法,它只选择不同来源的必需属性.源可以是数据库或内存中的集合.所以我定义了这样的扩展方法:
public IQueryable<TResult> SelectDynamic<TResult>(
this IQueryable<T> source,
...)
Run Code Online (Sandbox Code Playgroud)
这适用于IQueryables.但是,我也必须为IEnumerables 调用此函数.
在这种情况下,我可以在以下帮助下调用它.AsQueryable():
myEnumerable.AsQueryable()
.SelectDynamic(...)
.ToList();
Run Code Online (Sandbox Code Playgroud)
两者都很好.我有这样的问题,如果两者都工作正常,在哪些情况下我必须为同一目的创建两个不同的扩展方法,一个与之合作IEnumerable,另一个与IQueryable?
我的方法必须在以下情况下向数据库发送查询Queryable.
例如,以下是命名空间.Select 内的扩展方法的来源System.Linq:
我再次重复我的主要问题:
我的方法必须发送查询到数据库Queryable,但不是在使用时IEnumerable.而就目前而言,我正在使用AsQueryable()枚举器.因为,我不想为此编写相同的代码Enumerable.可以有一些副作用吗?
我想创建一个DDD存储库,它返回与Linq到SQL基础类匹配的IQueryable实体,减去任何关系.我可以轻松地返回实体减去与Linq选择新{field,field,...}投影的关系.如何编写Repository Entity类?如何从具有存储库类而不是Linq to SQL类的存储库中返回一个对象,并且仍然使用Linq选择中的多个实体填充它?我如何在ViewModel中引用此返回的类?
我对此很新,因此显而易见的错误.我是否错过了船,只应从存储库返回完整的实体,而不是投影?在从存储库发送之前,我仍然需要删除Linq to SQL关系.我完全不在基地吗?我真的想保留IQueryable数据类型.
例如,我的存储库中的Linq to SQL代码:
public class MiniProduct
{
public MiniProduct( string ProductNo, string Name, string Description, double Price)
{ this.ProductNo = ProductNo;
this.Name = Name;
this.Description = Description;
this.Price = Price;
}
}
public IQueryable<MiniProduct> GetProductsByCategory( string productCategory)
{
return ( from p in db.Products
from c in db.Categories
from pc in db.ProductCategories
where c.CategoryName == productCategory &&
c.CatID == pc.CatID &&
pc.ProductID == p.ProductID
select new { p.ProductNo, p.Name, p.Description, p.Price } ); …Run Code Online (Sandbox Code Playgroud) 是否可以访问IQueryable后面的DataContext对象?
如果是这样,怎么样?
我一直在学习IQueryable和延迟加载/延迟执行查询.
是否可以通过WCF公开此功能?我想暴露LINQ到SQL服务,返回一个IQueryable,我可以然后在客户端执行额外的查询,最后使用.ToList的execute().OData格式在这种情况下是否适用?
如果可能的话,究竟是什么技术术语,什么是一些很好的教程,我可以遵循?谢谢.
linq wcf iqueryable custom-linq-providers deferred-execution
我有一个接口,从Repository模式定义一个存储库:
interface IRepository
{
List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression);
}
Run Code Online (Sandbox Code Playgroud)
我已经针对Entity Framework实现了它:
class EntityFrameworkRepository
{
public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression)
{
return DBContext.Customers.Where(expression).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎运作良好,它允许我做类似的事情:
var customers = entityFrameworkRepository.Where(
customer => String.IsNullOrEmpty(customer.PhoneNumber)
);
Run Code Online (Sandbox Code Playgroud)
现在我想要一个InMemoryRepository用于测试和演示目的.我试图创建一个:
class InMemoryRepository
{
Dictionary<int, Customer> Customers {get; set;} = new Dictionary<int, Customer>();
public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression)
{
//what do I put here?
}
}
Run Code Online (Sandbox Code Playgroud)
正如您在上面的代码中看到的那样,我对InMemoryRepository.GetAllCustomers实现的操作感到困惑.我应该在那里用提供的表达式过滤Customers并返回结果?
我试过了:
return Customers.Where(expression));
Run Code Online (Sandbox Code Playgroud)
但很明显它是期待的,Func<KeyValuePair<int, Customer>, bool>所以我得到一个编译错误:
错误CS1929'Dictionary'不包含'Where'的定义和最佳扩展方法重载'Queryable.Where(IQueryable,Expression>)'需要类型为'IQueryable'DataAccess.InMemory的接收器
我们正在尝试将一个实例转换IQueryable<EntityObject>为a IQueryable<SpecificEntityObject>,该SpecificEntityObject类型仅在运行时已知.
我们尝试使用下面的代码,但由于类型或名称空间"objType"不存在,因此无法编译.
var t = query.ElementType;
Type objType = typeof(IQueryable<>).MakeGenericType(t);
var typed = query.Cast<IEnumerable<objType>>();
var grouped = typed.GroupByMany(groupBy.Select(grp => grp.Expression).ToArray());
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我已经看过这里的各种解决方案,但它们似乎都不适合我,可能是因为我对这一切都太新了,而且我在黑暗中摸索了一下.在下面的代码中,对象"约会"包含一些基本的LDAP信息.从这些对象的列表中,我希望能够根据员工ID获得单个记录.我希望这里的代码足以说明.FTR,我尝试了各种配方,包括尝试使用和选择.所有都失败,上面标题中给出了错误.
IQueryable<appointment> query = null;
foreach(var record in results)
{
BoiseStateLdapDataObject record1 = record;
query = db.appointments.Where(x => x.student_id == record1.Values["employeeid"]);
}
if (query != null)
{
var selectedRecord = query.SingleOrDefault();
}
Run Code Online (Sandbox Code Playgroud) iqueryable ×10
linq ×8
c# ×7
.net ×2
linq-to-sql ×2
casting ×1
datacontext ×1
entities ×1
func ×1
ienumerable ×1
repository ×1
wcf ×1