Tag: linq

LINQ中的`from..where`或`FirstOrDefault`

传统上,当我试图从数据库中获取用户的数据时,我使用了以下方法(在某种程度上): DbUsers curUser = context.DbUsers.FirstOrDefault(x => x.u_LoginName == id); string name = curUser.u_Name; string email = curUser.u_Email; 你可以看到我想要做的就是获取名称和电子邮件,但是在我看来,这个LINQ查询正在将所有的东西都存储在该用户的数据库中,然后将它带回来,然后让我得到我想要的。 我一直在做一些研究,并find了以下的select: var current = from s in context.DbUsers where s.u_LoginName == id select new { name = s.u_Name, email = s.u_Email }; foreach (var user in current) { //Stuff Here } 如果有的话,哪个更好? 当我只想检索几个结果/数据时,是否有一个更简单的方法?

LINQ在C#中join多个条件

我在C#中有一个LINQ Join语句,有多个条件。 var possibleSegments = from epl in eventPotentialLegs join sd in segmentDurations on new { epl.ITARequestID, epl.ITASliceNumber, epl.DepartAirportAfter, epl.AirportId_Origin, epl.AirportId_Destination } equals new { sd.ITARequestId, sd.SliceIndex, sd.OriginAirport, sd.DestinationAirport } where epl.DepartAirportAfter > sd.UTCDepartureTime and epl.ArriveAirportBy > sd.UTCArrivalTime select new PossibleSegments{ ArrivalTime = sd.arrivalTime }; join无法正常工作。 我究竟做错了什么?

Python的列表理解与.NET LINQ

以下简单的LINQ代码 string[] words = { "hello", "wonderful", "linq", "beautiful", "world" }; // Get only short words var shortWords = from word in words where word.Length <= 5 select word; // Print each word out shortWords.Dump(); 可以使用列表理解翻译成python如下。 words = ["hello", "wonderful", "linq", "beautiful", "world"] shortWords = [x for x in words if len(x) <=5] print shortWords […]

在C#中的F#List.map等效?

在C#中有没有相当于F#的List.map函数? 即对列表中的每个元素应用一个函数,并返回一个包含结果的新列表。 就像是: public static IEnumerable<TResult> Map<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> funky) { foreach (TSource element in source) yield return funky.Invoke(element); } 有没有内置的方式,或者我应该只写自定义扩展?

使用LINQ to SQL进行更新

我怎样才能更新logging对特定的ID在LINQ to SQL?

使用Linq来总结一个数字(并跳过其余的)

如果我们有一个包含这样一个数字的类: class Person { public string Name {get; set;} public int Amount {get; set;} } 然后是一群人: IList<Person> people; 那包含了,假设有10个随机名和数量的人是否有Linqexpression式,它会返回一个Person对象的子集合,其总和满足条件? 例如,我想要第一个x的人的金额总和是在1000以下。我可以做传统的 var subgroup = new List<Person>(); people.OrderByDescending(x => x.Amount); var count = 0; foreach (var person in people) { count += person.Amount; if (count < requestedAmount) { subgroup.Add(person); } else { break; } } 但我一直在想,是否有一个优雅的Linq做这样的事情使用总结,然后像Take一样的其他function? UPDATE […]

EF 6.x DbContext生成器的用途

我有一个使用Linq-to-SQL构build的Web应用程序,我正在将它升级到Linq-to-EF。 我已经看了一些教程,基本上在数据库第一场景中,你创build一个ADO.NET实体数据模型,然后从那里select要包含在模型中的表(与linq-to-sql非常相似)。 现在在Add New Item窗口中,我看到还有另一个选项,包括创build一个EF 6.x DbContext生成器。 与创buildADO.NET实体数据模型(窗口的第一个选项)相比,DbContext生成器的目的是什么?DbContext生成器的用途是什么? 它似乎创build一个文本文件; 我该怎么办呢?

为什么这是一个结构实现?

在System.Data.Linq中, EntitySet<T>使用了几个ItemList<T>结构,如下所示: internal struct ItemList<T> where T : class { private T[] items; private int count; …(methods)… } (花了我更多的时间来发现这一点 – 无法理解为什么EntitySet<T>的实体字段没有抛出空引用exception!) 我的问题是作为一个类的结构实现这个好处是什么?

权衡使用NHibernate 3.0 QueryOver或LINQ提供商

我还没有find与使用QueryOver语法相比NHibernate 3.0 LINQ提供程序支持的明确比较。 从表面上看,这似乎是两个非常类似的事情的两大努力。 什么是使用每个关键的权衡?

将IEnumerable转换/转换为IEnumerable <T>

我有一个类(一个Web控件)具有IEnumerabletypes的属性,并希望使用LINQ的参数。 有没有办法通过reflection投入/转换/调用IEnumerable <T>不知道编译时的types? Method void (IEnumerable source) { var enumerator = source.GetEnumerator(); if (enumerator.MoveNext()) { var type = enumerator.Current.GetType(); Method2<type>(source); // this doesn't work! I know! } } void Method2<T>(IEnumerable<T> source) {}