linq

Ignore global query filter for joined entities

喜夏-厌秋 提交于 2021-01-05 07:09:11
问题 Global query filters are very handy when implementing tenant and soft deletion features. But my problem is is that when i write a query with joins, for instance dbContext .entity1 .Include("entity2.entity3.entity4") .Where(something) .select(something) .toList(); and every one of those entities has global filters, then in the generated SQL i get for every JOIN a full Subquery where it selects all fields of that entity and checks for the global filters. But i dont want that. I want the global

Dapper(一) 简介和性能

ε祈祈猫儿з 提交于 2021-01-04 17:41:38
Dapper的简介   Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的。Dapper只有一个代码文件,完全开源,你可以放在项目里的任何位置,来实现数据到对象的ORM操作,体积小速度快。 使用ORM的好处是增、删、改很快,不用自己写sql,因为这都是重复技术含量低的工作,还有就是程序中大量的从数据库中读数据然后创建model,并为model字段赋值。这些ORM都可以轻松给你搞定。ORM给我们开发带来便利时,性能也是一个让我们不得不考虑的问题。一般的ORM性能和直接写原生的sql比都差不少,但是Dapper性能还很错,甚至和DbHelperSQL方式性能高出很多。 Dapper的优势 Dapper是一个轻型的ORM类。代码就一个SqlMapper.cs文件,编译后体积小。 Dapper很快。Dapper的速度接近与IDataReader,取列表的数据超过了DataTable。 Dapper支持多数据库。诸如:Mysql,SqlLite,Mssql系列,Oracle等一系列的数据库。 Dapper的R支持多表并联的对象。支持一对多 多对多的关系。并且没侵入性,想用就用,不想用就不用,无XML无属性,代码以前怎么写现在还怎么写。 Dapper原理通过Emit反射IDataReader的序列队列

C# Sql to Linq checking multiple cases

醉酒当歌 提交于 2021-01-04 07:07:43
问题 How to check multiple cases in Linq ( to classify "over paid","under paid","medium pay") Sql select id,name,salary, case when salary <=1500 then 'under paid' when salary >=3500 then 'over paid' else 'medium pay' end as status from Person Linq var q = context.Persons. Select(c => new { EmployeeID = c.Id, EmployeeName = c.name, EmployeeSalary = c.salary, Status = c.salary > 1000 ? "under paid" : "overpaid" } ); Using ternary operator i can check either or cases.otherwise i have to use if..else

LinQ to xml, specifying a route that can be null at any point

孤者浪人 提交于 2021-01-04 04:17:07
问题 <b> <c> <z> Wally </z> <w> whatever </w> </c> <d> <z> anotherValue </z> </d> <e> idValue </e> </b> class Example { public string Id {get; set;} public string name {get; set;} } Suppose we have the above xml and class. Im trying to create an object from the xml using LinQ to Xml. I could achieve this with something like this: var doc = XDocument.Load(xmlPath); var root = doc.Root; var anExample = new Example() { Id = root.Element("e").Value, name = root.Element("c").Element("z").Value }; So

LinQ to xml, specifying a route that can be null at any point

余生颓废 提交于 2021-01-04 04:16:55
问题 <b> <c> <z> Wally </z> <w> whatever </w> </c> <d> <z> anotherValue </z> </d> <e> idValue </e> </b> class Example { public string Id {get; set;} public string name {get; set;} } Suppose we have the above xml and class. Im trying to create an object from the xml using LinQ to Xml. I could achieve this with something like this: var doc = XDocument.Load(xmlPath); var root = doc.Root; var anExample = new Example() { Id = root.Element("e").Value, name = root.Element("c").Element("z").Value }; So

LinQ to xml, specifying a route that can be null at any point

青春壹個敷衍的年華 提交于 2021-01-04 04:14:44
问题 <b> <c> <z> Wally </z> <w> whatever </w> </c> <d> <z> anotherValue </z> </d> <e> idValue </e> </b> class Example { public string Id {get; set;} public string name {get; set;} } Suppose we have the above xml and class. Im trying to create an object from the xml using LinQ to Xml. I could achieve this with something like this: var doc = XDocument.Load(xmlPath); var root = doc.Root; var anExample = new Example() { Id = root.Element("e").Value, name = root.Element("c").Element("z").Value }; So

Does ToLookup forces immediate execution of a sequence

邮差的信 提交于 2021-01-03 06:50:02
问题 I was looking into Enumerable.ToLookup API which converts an enumerable sequence into a dictionary type data structure. More details can be found here: https://msdn.microsoft.com/en-us/library/system.linq.enumerable.tolookup(v=vs.110).aspx The only difference it carries from ToDictionary API is the fact that it won't give any error if the key selector results in duplicate keys. I need a comparison of deferred execution semantics of these two APIs. AFAIK ToDictionary API results in immediate

Does ToLookup forces immediate execution of a sequence

非 Y 不嫁゛ 提交于 2021-01-03 06:49:47
问题 I was looking into Enumerable.ToLookup API which converts an enumerable sequence into a dictionary type data structure. More details can be found here: https://msdn.microsoft.com/en-us/library/system.linq.enumerable.tolookup(v=vs.110).aspx The only difference it carries from ToDictionary API is the fact that it won't give any error if the key selector results in duplicate keys. I need a comparison of deferred execution semantics of these two APIs. AFAIK ToDictionary API results in immediate

并发编程概述--C#并发编程经典实例

痴心易碎 提交于 2021-01-02 23:39:27
优秀软件的一个关键特征就是具有并发性。过去的几十年,我们可以进行并发编程,但是难度很大。以前,并发性软件的编写、调试和维护都很难,这导致很多开发人员为图省事放弃了并发编程。新版.NET 中的程序库和语言特征,已经让并发编程变得简单多了。随着Visual Studio 2012 的发布,微软明显降低了并发编程的门槛。以前只有专家才能做并发编程,而今天,每一个开发人员都能够(而且应该)接受并发编程。 1.1简介 首先,我来解释几个贯穿本书始终的术语。先来介绍并发。 并发 同时做多件事情 这个解释直接表明了并发的作用。终端用户程序利用并发功能,在输入数据库的同时响应用户输入。服务器应用利用并发,在处理第一个请求的同时响应第二个请求。只要你希望程序同时做多件事情,你就需要并发。几乎每个软件程序都会受益于并发。大多数开发人员一看到“并发”就会想到“多线程”。对这两个概念,需要做一下区分。 多线程 并发的一种形式,它采用多个线程来执行程序。 从字面上看,多线程就是使用多个线程,多线程是并发的一种形式,但不是唯一的形式。实际上,直接使用底层线程类型在现代程序中基本不起作用。比起老式的多线程机制,采用高级的抽象机制会让程序功能更加强大、效率更高因此,这里尽量不涉及一些过时的技术。书中所有多线程的方法都采用高级类型,而不是Thread或BackgroundWorker。 一旦你输入new

Linq with optional WHERE options

戏子无情 提交于 2021-01-02 08:12:47
问题 I have a .Net function that accepts 3 parameters, all optional. Something like this: public List<MyObject> Search(string colour, string size, string name) { var result = (from c in MyTable where .... select c).ToList(); } My question is, what is the best way to do the where part. Would the best be to create dynamic linq? What's the best pattern, within linq, to have optional where parameters? So, in SQL, something like this: SELECT * FROM MyTable WHERE (@colour <> '' AND colour = @colour) AND