linq小知识总结
1linq的左连接查询
var boundList = from x in text.S_Outbound
join y in text.S_Outbound_Per
on x.Shipment_ID equals y.Shipment_ID into temp
from y in temp.DefaultIfEmpty()
select new
{
Id = x.Id,
perID = y.id == null ? : ,
Shipment_ID = x.Shipment_ID,//订单编号
Order_Type = y.Order_Type,//订单来源
Actual_Ship_Date_Time = y.Actual_Ship_Date_Time,//返回时间
USER_DEF2 = y.USER_DEF2,//快递单号
Ship_To_Name = x.Ship_To_Name,
Ship_To_Attention_To = x.Ship_To_Attention_To,
Ship_To_Address1 = x.Ship_To_Address1 + x.Ship_To_Address2 + x.Ship_To_Address3,
Ship_To_Phone_Num = x.Ship_To_Phone_Num,
Carrier_Services = x.Carrier_Services,
USER_DEF1 = x.USER_DEF1,//下单时间
};
2linq实现分页查询
(1)创建BaseQuery用于存储总数、页码和每页显示数量
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Model.Query
{
public class BaseQuery
{
public int PageIndex { get; set; }
public int PageSize { get; set; }
public int Total { get; set; }
}
}
(2)添加实体类的查询条件并继承BaseQuery
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Model.Query
{
public class ProductQuery : BaseQuery
{
public string ProductName { get; set; } }
}
(3)在数据访问层实现查询的方法
public IQueryable<Hishop_Products> LoadSearchData(ProductQuery query)
{
DbContext dbContext = new ShopEntities();
dbContext.Configuration.ProxyCreationEnabled = false;
var temp = dbContext.Set<Hishop_Products>().Where(u => true);
if (!string.IsNullOrEmpty(query.ProductName))
{
temp = temp.Where<Hishop_Products>(u => u.ProductName.Contains(query.ProductName));
}
#region 其他类型查询
////模糊查询
//temp = temp.Where<Hishop_Products>(u => u.ProductName.Contains(query.ProductName));
////普通查询
//temp = temp.Where<Hishop_Products>(u => u.ProductName.Equals(query.ProductName));
////值类型查询
//temp = temp.Where<Hishop_Products>(u => u.ProductName == query.ProductName);
#endregion
query.Total = temp.Count();
return temp.OrderBy(u => u.ProductId).Skip(query.PageSize * (query.PageIndex - )).Take(query.PageSize);
}
3利用BaseController重新封装数据并返回Json数据
public ContentResult JsonDate(object Date)
{
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" };
return Content(JsonConvert.SerializeObject(Date, Formatting.Indented, timeConverter));
}
4linq中自动求实体中某几个属性的累加和
var manager = from x in data
join y in text.SKUStock on x.skuId equals y.skuId into temp1
from y in temp1.DefaultIfEmpty()
select new
{
ProductId = x.ProductId,
CategoryId = x.CategoryId,
skuId = x.skuId,
sku = x.sku,
ProductName = x.ProductName,
ThumbnailUrl60 = x.ThumbnailUrl60,
stock = y != null ? y.F_Stock : ,
CostPrice = x.CostPrice,
SalePrice = x.SalePrice,
T_Stock = y != null ? y.T_Stock : ,
ValueStr = x.ValueStr,
All_stock = (y != null ? y.F_Stock : ) + (y != null ? y.T_Stock : ),
year = x.year
};
5屏蔽EF的自动导航用于解决EF+linq查出的数据循环引用的问题
text.Configuration.ProxyCreationEnabled = false;
text.Configuration.ValidateOnSaveEnabled = false;
6当数据库中字段太多并且想要将这些字段重新组合成一列显示的时候可以用以下方法实现
var ww = att.ToList().GroupBy(t => new { t.ProductId })
.Select(g => new
{
ProductId = g.Key.ProductId,
AttributeName = string.Join(",", g.Select(s => s.AttributeName).ToArray()),
ValueStr = string.Join(",", g.Select(s => s.ValueStr).ToArray())
});
7linq的条件查询+分页
/// <summary>
/// 分页
/// </summary>
/// <typeparam name="S">排序类型</typeparam>
/// <param name="pageIndex">页码</param>
/// <param name="pageSize">每页显示条数</param>
/// <param name="total">总数量</param>
/// <param name="whereLambda">lambda表达式(查询条件)</param>
/// <param name="isAsc">是否倒叙</param>
/// <param name="orderLambda">lambda表达式(排序条件)</param>
/// <returns></returns>
public IQueryable<T> PageList<S>(int pageIndex, int pageSize, out int total, Func<T, bool> whereLambda, bool isAsc, Func<T, S> orderLambda)
{
var temp = context.Set<T>().Where<T>(whereLambda);
total = temp.Count();
if (isAsc)
{
temp = temp.OrderBy<T, S>(orderLambda)
.Skip<T>(pageSize * (pageIndex - ))
.Take<T>(pageSize).AsQueryable();
}
else
{
temp = temp.OrderByDescending<T, S>(orderLambda)
.Skip<T>(pageSize * (pageIndex - ))
.Take<T>(pageSize).AsQueryable();
}
return temp.AsQueryable();
}
8linq+EF底层代码的实现
public T AddEntity(T model)
{
context.Set<T>().Attach(model);
context.Entry<T>(model).State = EntityState.Added;
dbSession.SaveChanges();
return model;
}
/// <summary>
/// 修改实体
/// </summary>
/// <param name="model">实体对象</param>
/// <returns>修改成功为true失败为false</returns>
public bool UpdateEntity(T model)
{
context.Set<T>().Attach(model);
context.Entry<T>(model).State = EntityState.Modified;
return dbSession.SaveChanges() > ;
}
/// <summary>
/// 删除实体
/// </summary>
/// <param name="model">实体对象</param>
/// <returns>修改成功为true失败为false</returns>
public bool DeleteEntity(T model)
{
context.Set<T>().Attach(model);
context.Entry<T>(model).State = EntityState.Deleted;
return dbSession.SaveChanges() > ;
} /// <summary>
/// 根据条件查询实体集合
/// </summary>
/// <param name="whereLambda">lambda表达式(查询条件)</param>
/// <returns>查询到的集合</returns>
public IQueryable<T> SelectByWhere(Func<T, bool> whereLambda)
{
return context.Set<T>().Where<T>(whereLambda).AsQueryable();
}
linq小知识总结的更多相关文章
- [C#.NET 拾遗补漏]02:数组的几个小知识
阅读本文大概需要 1.5 分钟. 数组本身相对来说比较简单,能想到的可写的东西不多.但还是有一些知识点值得总结和知晓一 下.有的知识点,知不知道不重要,工作中用的时候搜索一下就可以了,毕竟实现一个功 ...
- 蓝牙Bluetooth技术小知识
蓝牙Bluetooth技术以及广泛的应用于各种设备,并将继续在物联网IoT领域担任重要角色.下面搜集整理了一些关于蓝牙技术的小知识,以备参考. 蓝牙Bluetooth技术始创于1994年,其名字来源于 ...
- HTML+CSS中的一些小知识
今天分享一些HTML.CSS的小知识,希望能够对大家有所帮助! 1.解决网页乱码的问题:最重要的是要保证各个环节的字符编码一致! (1)编辑器的编辑环境的字符集(默认字符集):Crtl+U 常见的编码 ...
- iOS APP开发的小知识(分享)
亿合科技小编发现从2007年第一款智能手机横空出世,由此开启了人们的移动智能时代.我们从一开始对APP的陌生,到现在的爱不释手,可见APP开发的出现对我们的生活改变有多巨大.而iOS AP ...
- Unix系统小知识(转)
Unix操作系统的小知识 2.VI添加行号/翻页/清屏 .在对话模式时(即输完Esc再输入: ),输入“:set number”可以将编辑的文本加上行号.跟玩俄罗斯方块一样方便的上下左右移动箭头的快捷 ...
- salesforce 零基础开发入门学习(十)IDE便捷小知识
在这里介绍两个IDE的便捷开发的小知识. 一) 本地调试 由于salesforce代码只能提交以后才能调试,所以很多时候调试代码很麻烦.新版增加了一个特性:即可以在本地调试相关的代码或者查看相关代码运 ...
- Jquery:小知识;
Jquery:小知识: jQuery学习笔记(二):this相关问题及选择器 上一节的遗留问题,关于this的相关问题,先来解决一下. this的相关问题 this指代的是什么 这个应该是比较好理 ...
- HTML小知识---Label
今天知道了一个html小知识: <input type="checkbox" id="chkVersion" /> ...
- Unicode和汉字编码小知识
Unicode和汉字编码小知识 将汉字进行UNICODE编码,如:“王”编码后就成了“\王”,UNICODE字符以\u开始,后面有4个数字或者字母,所有字符都是16进制的数字,每两位表示的256以内的 ...
随机推荐
- Machine Learning & Data Mining 资料整合
机器学习常见算法分类汇总 | 码农网 数据挖掘十大经典算法 | CSDN博客 (内含十个算法具体介绍) 支持向量机通俗导论(理解 SVM 的三层境界)| CSDN博客 (强烈推荐关注博主) 教你如何迅 ...
- 使用Async同步执行异步函数
为了适应异步编程,减少回调的嵌套,我在项目中引入了Async,当批量处理且需要同步执行一些逻辑相同的异步函数时,觉得还是Async最为靠谱. 我有一个类似下面代码的场景,依据数组中的每一个元素执行一个 ...
- NotePad++ delphi/Pascal函数过程列表插件
从cnpack上爬下来的 函数过程列表 点击下载
- [转]SQL中char、varchar、nvarchar的区别
char char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符. nvarcha ...
- Spark RDD概念学习系列之Spark的算子的作用(十四)
Spark的算子的作用 首先,关于spark算子的分类,详细见 http://www.cnblogs.com/zlslch/p/5723857.html 1.Transformation 变换/转换算 ...
- HDU 4893 Wow! Such Sequence! (线段树)
Wow! Such Sequence! 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4893 Description Recently, Doge ...
- spring注解使用
一.各种注解方式 1.@Autowired注解(不推荐使用,建议使用@Resource) @Autowired可以对成员变量.方法和构造函数进行标注,来完成自动装配的工作.@Autowired的标注位 ...
- nyoj 20 吝啬的国度
吝啬的国度 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...
- Codeforces 626A Robot Sequence
A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 用ALAssetsLibrary将过滤后图片写入照片库
转载自:http://blog.sina.com.cn/s/blog_61235faa0100z3dp.html CIImage *saveToSave = [filter outputImage]; ...