linq

NewtonSoft JArray - how to select multiple elements with LINQ

ぃ、小莉子 提交于 2020-08-08 07:20:11
问题 I have some JSON which I then parse to JArray object. I want to filter the JArray so it then has only two properties, Title and BodyText. But whatever I try I can only select one value using LINQ. [HttpGet] public JsonResult AjaxGetNewsItems() { string json = JsonConvert.SerializeObject(news.GetNewsItems(), formatting:Formatting.Indented); var v = JArray.Parse(json); //var items = // v.Where( // x => // x["Title"].ToString() != string.Empty && // x["BodyText"].ToString() != string.Empty) //

Asp.NetCore3.1 WebApi 使用Jwt 授权认证使用

眉间皱痕 提交于 2020-08-08 05:19:10
1:导入NuGet包 Microsoft.AspNetCore.Authentication.JwtBearer 2:配置 jwt相关信息 3:在 startUp中 1 public void ConfigureServices(IServiceCollection services){ 2 #region JWT 认证 3 services 4 .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) 5 .AddJwtBearer(options => { 6 var jsonmodel = AppJsonHelper.InitJsonModel(); 7 options.TokenValidationParameters = new TokenValidationParameters 8 { 9 ValidIssuer = jsonmodel.Issuer, // Configuration["JwtSetting:Issuer"], 10 ValidAudience = jsonmodel.Audience, // Configuration["JwtSetting:Audience"], 11 // IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8

Convert multi-dimensional collection to string with separator

情到浓时终转凉″ 提交于 2020-08-08 05:15:54
问题 As I was working on this answer, I've written some code to convert a generic multi-dimensional collection to a string. public static string ConvertToString<T>(this IEnumerable<IEnumerable<T>> input, string columnSplit = "", string rowSplit = "\n") { return string.Join(rowSplit, input.Select(r => string.Concat(string.Join(columnSplit, r.Select(c => c.ToString()))))); } Example Input IEnumerable<IEnumerable<string>> input = new List<List<string>> { new List<string> { "R", "L", "R", "R" }, new

Dynamic LINQ - Struggling with GROUP BY / SELECT Syntax

亡梦爱人 提交于 2020-08-08 04:00:48
问题 I have a repeating set of data that I'm retrieving from multiple SQL tables (long story but they all have different names despite having the same data) into a .NET DataTable:- Point_Date -> Point_Value0 -> Point_Value1 -> Point_Value2 -> Point_ValueX 24/11/2014 16:18:07 -> 15.1 -> NULL -> NULL 24/11/2014 16:19:07 -> 15.2 -> NULL -> NULL 24/11/2014 16:20:07 -> 15.3 -> NULL -> NULL 24/11/2014 16:18:07 -> NULL -> 16.1 -> NULL 24/11/2014 16:19:07 -> NULL -> 16.2 -> NULL 24/11/2014 16:20:07 ->

Dynamic LINQ - Struggling with GROUP BY / SELECT Syntax

怎甘沉沦 提交于 2020-08-08 04:00:47
问题 I have a repeating set of data that I'm retrieving from multiple SQL tables (long story but they all have different names despite having the same data) into a .NET DataTable:- Point_Date -> Point_Value0 -> Point_Value1 -> Point_Value2 -> Point_ValueX 24/11/2014 16:18:07 -> 15.1 -> NULL -> NULL 24/11/2014 16:19:07 -> 15.2 -> NULL -> NULL 24/11/2014 16:20:07 -> 15.3 -> NULL -> NULL 24/11/2014 16:18:07 -> NULL -> 16.1 -> NULL 24/11/2014 16:19:07 -> NULL -> 16.2 -> NULL 24/11/2014 16:20:07 ->

C# LINQ replacing nulls with meaningful string

大憨熊 提交于 2020-08-08 03:56:32
问题 From the list class Delivery { public string ProductCode { get; set; } public DateTime? OrderedDate { get; set; } public DateTime? DeliveryDate { get; set; } public Delivery(string pcode, DateTime? orddate, DateTime? deldate) { ProductCode = pcode; OrderedDate = orddate; DeliveryDate = deldate; } } List<Delivery> DeliveryList = new List<Delivery>(); DeliveryList.Add(new Delivery("P001",new DateTime(2009,01,27),null)); DeliveryList.Add(new Delivery("P007",new DateTime(2009,05,17),null));

LINQ聚合算法解释

橙三吉。 提交于 2020-08-07 12:22:09
问题: This might sound lame, but I have not been able to find a really good explanation of Aggregate . 这可能听起来很蹩脚,但我还没有找到一个关于 Aggregate 的非常好的解释。 Good means short, descriptive, comprehensive with a small and clear example. 良好意味着简短,描述性,全面,有一个小而明确的例子。 解决方案: 参考一: https://stackoom.com/question/ToSv/LINQ聚合算法解释 参考二: https://oldbug.net/q/ToSv/LINQ-Aggregate-algorithm-explained 来源: oschina 链接: https://my.oschina.net/u/3797416/blog/4340110

在ASP.NET Core MVC Action中判断某个视图是否存在

不问归期 提交于 2020-08-07 07:28:16
原文: 在ASP.NET Core MVC Action中判断某个视图是否存在 在开发Web网站过程中可能会遇到需要添加许多宣传页,这些宣传页往往不需要什么后端逻辑代码,这时候我们就不希望为每一个宣传页都添加一个Action, 而是希望只添加一个Action,然后结合路由动态的指向不同的视图。而动态的指向不同的视图这时候可能就需要用到判断某个视图是否存在,不存在执行某个逻辑。 下面我们来看下Demo: 首先来看下Demo的项目结构 接下来看下核心代码 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ViewEngines; namespace IsExistsViewDemo.Controllers { /// <summary> /// 静态宣传页 /// </summary> public class DHtmlController : Controller { #region 字段和属性 /// <summary> /// 混合视图引擎 /// </summary> private readonly

EFcore 修改部分字段

吃可爱长大的小学妹 提交于 2020-08-07 03:10:17
using System.Linq.Expressions; // 用表达式树,部分字段 Expression<Func<CourseSchedule, object >>[] updatedProperties = { p => p.createtime, }; 调用Helper类 _courseScheduleRepository.Value.UpdateEntity(schedule, updatedProperties, true ); Helper类 /// <summary> /// 更新部分字段 /// </summary> public virtual int UpdateEntity(T entity, Expression<Func<T, object >>[] updatedProperties, bool IsCommit = true ) { int result = 0 ; _dbContext.Set <T> ().Attach(entity); if (updatedProperties.Any()) { foreach ( var property in updatedProperties) { _dbContext.Entry <T>(entity).Property(property).IsModified = true ; } } if

FreeSql (三十四)CodeFirst 迁移说明

。_饼干妹妹 提交于 2020-08-06 19:53:59
FreeSql 支持 CodeFirst 迁移结构至数据库,这应该是(O/RM)必须标配的一个功能。 与其他(O/RM)不同FreeSql支持更多的数据库特性,而不只是支持基础的数据类型,这既是优点也是缺点,优点是充分利用数据库特性辅助开发,缺点是切换数据库变得困难。不同程序员的理念可能不太一致,FreeSql尽量把功能支持到极致,至于是否使用是项目组技术衡量的另一个问题。 尽管多种数据库适配逻辑非常复杂,FreeSql始终秉承优化程序开发习惯的原则尽量去实现,中间碰到了一些非技术无法攻克的难题,比如数据库的自定义类型,和实体类本身就是一种冲突,为了减少使用成本,诸如此类的数据库功能没有得到支持。 static IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10") .UseAutoSyncStructure(true) //自动同步实体结构【开发环境必备】 .Build(); //请务必定义成