Tag: linq

在NHibernate Linq提供程序中获取vs FetchMany

NHibernate预先加载可以使用Fetch和FetchMany ,如NHibernate Linq Eager在Mike Hadlow的博客上的描述。 这两种方法之间有什么区别,在什么情况下都会使用?

如何获得每个组使用Linq的第一个logging

考虑到以下logging: Id F1 F2 F3 ————————————————- 1 Nima 1990 10 2 Nima 1990 11 3 Nima 2000 12 4 John 2001 1 5 John 2002 2 6 Sara 2010 4 我想根据F1字段进行分组并按照Idsorting,并从类似这些logging的组的第一个logging中获取所有字段: Id F1 F2 F3 ————————————————- 1 Nima 1990 10 4 John 2001 1 6 Sara 2010 4 我如何使用linq做到这一点?

使用LINQ将项目移动到列表的顶部

有没有办法使用LINQ将一个say id = 10的项目作为列表中的第一个项目? 项目A – id = 5 项目B – id = 10 项目C – id = 12 项目D – id = 1 在这种情况下,我该如何优雅地将项目C移动到List<T>集合的顶部? 这是我现在最好的: var allCountries = repository.GetCountries(); var topitem = allCountries.Single(x => x.id == 592); var finalList = new List<Country>(); finalList.Add(topitem); finalList = finalList.Concat(allCountries.Where(x=> x.id != 592)).ToList();

自从创build数据库以来,支持“ApplicationDbContext”上下文的模型已经发生了变化

首先我还没有看到这个错误,我猜这不是一个复制品,所以请先阅读整个情况 每一件事情都工作得很好,然后我试图更新我的模型类 ( 应用程序类和更新现在左注释),我将在下面列出和繁荣,我有这个丑陋的错误。 自从创build数据库以来,支持“ApplicationDbContext”上下文的模型已经发生了变化。 考虑使用Code First Migrations来更新数据库( http://go.microsoft.com/fwlink/?LinkId=238269 )。 在System.Data.Entity.CreateDatabaseIfNotExists 1.InitializeDatabase(TContext context) at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf 1.b_ e()at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action )System.Data.Entity.Internal.LazyInternalContext.b中的System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()System.Data.Entity.Internal.RetryAction中的System.Data.Entity.Internal.PerformDatabaseInitialization()上的System.Data.Entity.Internal 1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(ActiontypesentityType)在System.Data System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() 1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action 1操作) .Inntity.Internal.Linq.InternalSet 1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet 1.Include(String path)在System.Data.Entity.Infrastructure.DbQuery 1.Include(String path) at System.Data.Entity.QueryableExtensions.Include[T](IQueryable 在Microsoft.AspNet.Identity.EntityFramework.UserStore 6.GetUserAggregateAsync(Expression 1filter)在System.Data.Entity.QueryableExtensions.Include [T,TProperty](IQueryable 1 source, Expression 1path) Microsoft.AspNet.Identity.EntityFramework.UserStore 6.FindByNameAsync(String userName) at Microsoft.AspNet.Identity.UserManager 2.FindByNameAsync(String userName)at […]

无法将lambdaexpression式转换为“string”types,因为它不是委托types

我正在使用像这样的LINQ lambdaexpression式: int Value = 1; qryContent objContentLine; using (Entities db = new Entities()) { objContentLine = (from q in db.qryContents where q.LineID == Value orderby q.RowID descending select q).FirstOrDefault(); } 但是,我收到以下错误: 无法将lambdaexpression式转换为“string”types,因为它不是委托types

AsQueryable()的目的是什么?

AsQueryable()的目的只是为了让你可以将IEnumerable传递给可能期望IQueryable方法,还是有一个有用的理由将IEnumerable表示为IQueryable ? 例如,它应该是这样的情况: IEnumerable<Order> orders = orderRepo.GetAll(); // I don't want to create another method that works on IEnumerable, // so I convert it here. CountOrders(orders.AsQueryable()); public static int CountOrders(IQueryable<Order> ordersQuery) { return ordersQuery.Count(); } 还是它实际上使它做了不同的事情: IEnumerable<Order> orders = orderRepo.GetAll(); IQueryable<Order> ordersQuery = orders.AsQueryable(); IEnumerable<Order> filteredOrders = orders.Where(o => o.CustomerId == 3); IQueryable<Order> filteredOrdersQuery = […]

为什么IEnumerable <T> .ToList <T>()返回List <T>而不是IList <T>?

扩展方法ToList()返回一个List<TSource> 。 ToDictionary()返回一个Dictionary<TKey, TSource> 。 我很好奇为什么这些方法不分别inputIList<TSource>和IDictionary<TKey, TSource>的返回值。 这似乎更奇怪,因为ToLookup<TSource, TKey>其返回值作为接口而不是实际的实现。 使用dotPeek或其他反编译器查看这些扩展方法的来源,我们看到下面的实现(显示ToList()因为它更短): public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source) { if (source == null) throw Error.ArgumentNull("source"); return new List<TSource>(source); } 那么为什么这种方法将其返回值作为接口的特定实现而不是接口本身呢? 唯一的变化是返回types。 我很好奇,因为IEnumerable<>扩展名在它们的签名中非常一致,除了这两种情况。 我一直认为这有点奇怪。 另外,为了使事情更令人困惑, ToLookup()的文档声明: 根据指定的键select器函数从IEnumerable创build一个Lookup。 但返回types是ILookup<TKey, TElement> 。 在Edulinq中 ,Jon Skeet提到返回types是List<T>而不是IList<T> ,但是并没有进一步触及这个主题。 广泛的search没有得到答案,所以在这里我问你: 是否有任何devise决定背后没有键入作为接口的返回值,还是只是偶然?

Linq到对象:GroupBy是否保存元素的顺序?

Enumerable.GroupBy从LINQ到对象保存组中的元素的顺序?

LINQ不同的运算符,忽略大小写?

给出以下简单的例子: List<string> list = new List<string>() { "One", "Two", "Three", "three", "Four", "Five" }; CaseInsensitiveComparer ignoreCaseComparer = new CaseInsensitiveComparer(); var distinctList = list.Distinct(ignoreCaseComparer as IEqualityComparer<string>).ToList(); 看来CaseInsensitiveComparer实际上并没有被用来做大小写不敏感的比较。 换句话说, distinctList包含与列表相同数量的项目。 相反,我认为,例如,“三”和“三”被认为是平等的。 我是否错过了一些东西,或者这是一个Distinct运算符的问题?

“折叠”LINQ扩展方法在哪里?

我在MSDN的Linq中find了一个我想使用的叫做Fold()的整洁方法。 他们的例子: double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; double product = doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor); 不幸的是,我无法得到这个编译,无论是在他们的例子或我自己的代码,我找不到在MSDN其他地方(如Enumerable或数组扩展方法)提到这种方法。 我得到的错误是一个普通的老“不知道任何关于”的错误: error CS1061: 'System.Array' does not contain a definition for 'Fold' and no extension method 'Fold' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or […]