Tag: linq

在C#中逐行读取文件

我正在尝试阅读一些文本文件,每行需要处理。 目前,我只是使用StreamReader,然后逐一读取每一行。 我想知道是否有一个更有效的方法(在LoC和可读性方面)使用LINQ来做到这一点,而不会影响操作效率。 我所看到的例子涉及将整个文件加载到内存中,然后处理它。 不过,在这种情况下,我不认为这将是非常有效的。 在第一个例子中,文件可以达到大约50k,而在第二个例子中,不需要读取文件的所有行(大小通常<10k)。 你可以争辩说,现在这些小文件并不重要,但是我相信这种方法会导致代码效率低下。 第一个例子: // Open file using(var file = System.IO.File.OpenText(_LstFilename)) { // Read file while (!file.EndOfStream) { String line = file.ReadLine(); // Ignore empty lines if (line.Length > 0) { // Create addon T addon = new T(); addon.Load(line, _BaseDir); // Add to collection collection.Add(addon); } } } 第二个例子: // […]

独特的不使用LINQ到对象

class Program { static void Main(string[] args) { List<Book> books = new List<Book> { new Book { Name="C# in Depth", Authors = new List<Author> { new Author { FirstName = "Jon", LastName="Skeet" }, new Author { FirstName = "Jon", LastName="Skeet" }, } }, new Book { Name="LINQ in Action", Authors = new List<Author> { new […]

LINQ to Entities不能识别方法System.String ToString()方法,并且这个方法不能被转换成存储expression式

我正在从一个MySQL服务器迁移一些东西到一个SQL服务器,但我不知道如何使这个代码工作: using (var context = new Context()) { … foreach (var item in collection) { IQueryable<entity> pages = from p in context.pages where p.Serial == item.Key.ToString() select p; foreach (var page in pages) { DataManager.AddPageToDocument(page, item.Value); } } Console.WriteLine("Done!"); Console.Read(); } 当它进入第二个foreach (var page in pages)它会抛出一个exception说: LINQ to Entities不能识别方法System.String ToString()方法,并且此方法不能被转换成存储expression式。 任何人都知道为什么会这样

不同于linq的类的属性

我有一个集合: List<Car> cars = new List<Car>(); 汽车是由他们的财产CarCode唯一标识。 我有三辆车在收集,和两个相同的CarCodes。 我如何使用LINQ将这个集合转换成独特的CarCodes的汽车?

entity framework:已经有一个开放的DataReader与这个Command相关联

我正在使用entity framework,偶尔我会得到这个错误。 EntityCommandExecutionException {"There is already an open DataReader associated with this Command which must be closed first."} at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands… 即使我没有做任何手动连接pipe理。 这个错误间歇发生。 触发错误的代码(为便于阅读而缩短): if (critera.FromDate > x) { t= _tEntitites.T.Where(predicate).ToList(); } else { t= new List<T>(_tEntitites.TA.Where(historicPredicate).ToList()); } 使用Dispose模式,以便每次打开新的连接。 using (_tEntitites = new TEntities(GetEntityConnection())) { if (critera.FromDate > x) { t= _tEntitites.T.Where(predicate).ToList(); } else { t= […]

Linq to Sql:多个左外连接

我遇到了一些麻烦,弄清楚如何使用LINQ to SQL使用多个左外连接。 我明白如何使用一个左外连接。 我正在使用VB.NET。 以下是我的SQL语法。 T-SQL SELECT o.OrderNumber, v.VendorName, s.StatusName FROM Orders o LEFT OUTER JOIN Vendors v ON v.Id = o.VendorId LEFT OUTER JOIN Status s ON s.Id = o.StatusId WHERE o.OrderNumber >= 100000 AND o.OrderNumber <= 200000

EntityFramework中的ObjectSet包含多less个可用于保持性能?

我正在使用以下的LINQ查询我的个人资料页面: var userData = from u in db.Users .Include("UserSkills.Skill") .Include("UserIdeas.IdeaThings") .Include("UserInterests.Interest") .Include("UserMessengers.Messenger") .Include("UserFriends.User.UserSkills.Skill") .Include("UserFriends1.User1.UserSkills.Skill") .Include("UserFriends.User.UserIdeas") .Include("UserFriends1.User1.UserIdeas") where u.UserId == userId select u; 它有一个很长的对象图,并使用许多包括。 它现在运行的很完美,但是当网站有很多用户时,会不会影响性能呢? 我应该以其他方式做吗?

LINQ to SQL左外部连接

这个查询是否等价于一个LEFT OUTER连接? //assuming that I have a parameter named 'invoiceId' of type int from c in SupportCases let invoice = c.Invoices.FirstOrDefault(i=> i.Id == invoiceId) where (invoiceId == 0 || invoice != null) select new { Id = c.Id , InvoiceId = invoice == null ? 0 : invoice.Id }

如何做像SQL LINUX中的%?

我有一个SQL程序,我正在试图变成Linq: SELECT O.Id, O.Name as Organization FROM Organizations O JOIN OrganizationsHierarchy OH ON O.Id=OH.OrganizationsId where OH.Hierarchy like '%/12/%' 我最关心的是: where OH.Hierarchy like '%/12/%' 我有一个存储像/ 1/3/12 /这样的层次结构的列,所以我只是使用%/ 12 /%来search它。 我的问题是,什么是Linq或.NET等效于使用百分号?

LINQ到SQL与存储过程?

我在“StackOverflow”( “LINQ初学者指南” )这篇文章中看了一下“LINQ 入门指南” ,但有一个后续问题: 我们即将开始一个新的项目,几乎所有的数据库操作都将是相当简单的数据检索(还有另一个项目已经写入数据)。 到目前为止,我们大部分的其他项目都使用存储过程来处理这些事情。 不过,如果更合理的话,我想利用LINQ到SQL。 所以,问题是这样的:对于简单的数据检索,哪种方法更好,LINQ到SQL或存储过程? 任何具体的赞成或反对? 谢谢。