linq

Custom Linq Ordering

蓝咒 提交于 2021-02-06 11:11:50
问题 I have over a thousand folders, each folder contains one or more files with the following names: Unordered: Alison.ext Heather.ext Molly.ext Paula.ext Sam.ext Ordered: Molly.ext Sam.ext Heather.ext Alison.ext Paula.ext I would like to write an expression to sort this list as described above. 回答1: //Creating a dictionary with the custom order var order = "MSHAP"; var orderDict = order.Select((c,i)=>new {Letter=c, Order=i}) .ToDictionary(o => o.Letter, o => o.Order); var list = new List<string>

Custom Linq Ordering

让人想犯罪 __ 提交于 2021-02-06 11:07:29
问题 I have over a thousand folders, each folder contains one or more files with the following names: Unordered: Alison.ext Heather.ext Molly.ext Paula.ext Sam.ext Ordered: Molly.ext Sam.ext Heather.ext Alison.ext Paula.ext I would like to write an expression to sort this list as described above. 回答1: //Creating a dictionary with the custom order var order = "MSHAP"; var orderDict = order.Select((c,i)=>new {Letter=c, Order=i}) .ToDictionary(o => o.Letter, o => o.Order); var list = new List<string>

Custom Linq Ordering

删除回忆录丶 提交于 2021-02-06 11:06:01
问题 I have over a thousand folders, each folder contains one or more files with the following names: Unordered: Alison.ext Heather.ext Molly.ext Paula.ext Sam.ext Ordered: Molly.ext Sam.ext Heather.ext Alison.ext Paula.ext I would like to write an expression to sort this list as described above. 回答1: //Creating a dictionary with the custom order var order = "MSHAP"; var orderDict = order.Select((c,i)=>new {Letter=c, Order=i}) .ToDictionary(o => o.Letter, o => o.Order); var list = new List<string>

C#代码优化:斩断伸向堆内存的“黑手”

断了今生、忘了曾经 提交于 2021-02-06 10:28:27
在上期 《C#代码优化:拯救你的CPU耗时》 中,我们依托 UWA本地资源检测 ,从“时间”的角度对C#代码检测中和CPU耗时相关的知识点为大家进行了简单的讲解。本篇将从“空间”角度入手,为大家继续梳理C#代码检测的相关规则。 C#是在虚拟机(VM)环境当中运行的(Mono虚拟机或IL2CPP虚拟机),其分配的堆内存是由虚拟机进行管理与回收的,因此,C#代码被称为“托管代码”,其堆内存又被称为“托管内存”。托管内存的释放依赖于虚拟机的GC(垃圾回收)机制,本文就不展开讨论了。 C#程序开发要遵循的一个基本原则就是避免不必要的堆内存分配,而堆内存分配主要会造成以下后果: 程序所占用的内存总量过大。内存是有限的资源,对于游戏(特别是移动游戏)来说,内存的占用可谓是寸土寸金。过多的无法释放的内存很可能导致程序崩溃的现象。 过多的分配次数会导致堆碎片变多,从而可能导致无法开辟出所需要的连续的内存,这也会导致程序崩溃。 内存分配会触发GC(垃圾回收),而GC的代价较高,会造成卡顿。 1、该类的方法中存在 .tag的调用 tag是场景中GameObject的标签,而类GameObject的成员tag是一个属性,在获取该属性时,实质上是调用get_tag()函数,从native层返回一个字符串。字符串属于引用类型,这个字符串的返回,会造成堆内存的分配。然而

Table Valued Function and Entity Framework

时光毁灭记忆、已成空白 提交于 2021-02-06 08:47:48
问题 I'm trying to execute an TVF with Entity Framework and for some reason it just doesn't work. Maybe anyone out there can help me see the problem. Here are the code samples: That's the function: CREATE FUNCTION [dbo].[udf_profileSearch] (@keywords NVARCHAR(3000)) RETURNS @results TABLE ( [Id] [int] NULL, [SubCategoryId] [int] NULL, [UserId] [int] NULL, [SmallDescription] [nvarchar](250) NULL, [DetailedDescription] [nvarchar](500) NULL, [Graduation] [nvarchar](140) NULL, [Experience] [nvarchar]

Table Valued Function and Entity Framework

只谈情不闲聊 提交于 2021-02-06 08:47:18
问题 I'm trying to execute an TVF with Entity Framework and for some reason it just doesn't work. Maybe anyone out there can help me see the problem. Here are the code samples: That's the function: CREATE FUNCTION [dbo].[udf_profileSearch] (@keywords NVARCHAR(3000)) RETURNS @results TABLE ( [Id] [int] NULL, [SubCategoryId] [int] NULL, [UserId] [int] NULL, [SmallDescription] [nvarchar](250) NULL, [DetailedDescription] [nvarchar](500) NULL, [Graduation] [nvarchar](140) NULL, [Experience] [nvarchar]

SingleOrDefault() throws an exception on more than one element

☆樱花仙子☆ 提交于 2021-02-06 08:47:18
问题 I'm getting an exception whenever I fetch like this Feature f = o.Features.SingleOrDefault(e => e.LinkName == PageLink); because this can return one or more than one element. What is the alternative approach that I can use to solve this issue? 回答1: Single and SingleOrDefault are designed to throw if more that one match exists in the sequence. A consequence of this is that the entire sequence must be iterated prior to completion. It does not sound like this is what you want. Try FirstOrDefault

SingleOrDefault() throws an exception on more than one element

会有一股神秘感。 提交于 2021-02-06 08:46:00
问题 I'm getting an exception whenever I fetch like this Feature f = o.Features.SingleOrDefault(e => e.LinkName == PageLink); because this can return one or more than one element. What is the alternative approach that I can use to solve this issue? 回答1: Single and SingleOrDefault are designed to throw if more that one match exists in the sequence. A consequence of this is that the entire sequence must be iterated prior to completion. It does not sound like this is what you want. Try FirstOrDefault

NullReferenceException in EF Core when using StringComparison overload for predicates

戏子无情 提交于 2021-02-05 11:46:53
问题 We are converting a project from using EF to using EF Core. We have the following line of code which used to work but now does not: // throws NullReferenceException var u = db.Users.FirstOrDefault(x => x.PresenterID.Equals(uid, StringComparison.OrdinalIgnoreCase)); However, if we don't use the StringComparison overload, it works: // this works var u = db.Users.FirstOrDefault(x => x.PresenterID.Equals(uid)); This is a large project and we would like to avoid finding and modifying all code that

making a list using inner query in Linq in C#

二次信任 提交于 2021-02-05 11:26:47
问题 I am trying to create a sublist using Linq but don't understand the error in this. I don't think i am doing wrong but i think others eye will help me to sort this issue. var dataList = File.ReadAllLines(inputFile); dataList = from line in dataList let temp = from data in line.Split(';').ToList() where line.Split(';').ToList().IndexOf(data) != 0 ||line.Split(';').ToList().IndexOf(data) != 1 select data select string.Join(",",temp); I am getting error saying that IEnumerable list cannot be