linq

How to search a varchar field, using LINQ, to build a list of recommendations

為{幸葍}努か 提交于 2021-02-10 20:24:13
问题 I am trying to construct a LINQ query, with expression trees, to do the following: I have a field in my ItemCode table called Labels, an example of the data contained in this field is "lamps lighting chandelier". I want to allow the user to type in some text, i.e. "Red Lamp", and be able to search the Labels field, of the ItemCode, table where the text contains "Red" or "Lamp". I am trying to recommend selections to the user, and this, while basic, is a good first step ... just need some help

Changing DateTime format in lambda Expression

自闭症网瘾萝莉.ら 提交于 2021-02-10 16:02:37
问题 I have a DTO field in DateTime format public DateTime date_joined { get; set; } I use this to turn the data into a Json public JsonResult Customer(int? id) { var user = User.Identity.Name; if (!AccountController.IsInRole(user, "admin")) { return null; } id = id == null ? 0 : id; var customer = db.PrmCustomers .Where(x => x.EmailAddress != null && x.CustomerID > id) .OrderBy(x => x.CustomerID) .Select(x => new CustomerDTO() { email = x.EmailAddress, phone = x.MobilePhoneNumber, username = "",

C# Linq Lambda Expression for Entity Framework Query Passing Custom Expression to Where Condition

流过昼夜 提交于 2021-02-10 14:51:10
问题 I have two tables called ShipmentType and Books. Entity class has been mapped for these tables. Created another class called BookShipment which contains two properties, classes of ShipmentType and Book. public class BookShipment { public ShipmentType Shipment { get; set; } public Books Book { get; set; } } I was trying to create a where expression as follows. Expression<Func<BookShipment, bool>> expr = x => (x.Shipment.ID == 1 && x.Book.ID == 1); var result = from c in styp join d in book on

How can I group/sum values in LINQ to XML?

心已入冬 提交于 2021-02-10 14:24:44
问题 I've been trying to work thru this but I am having problems grouping the data. I have an XML structure <FILE> <PLAN> <PLAN_ID>001</PLAN_ID> <PARTICIPANT> <PARTICIPANT_ID>9999</PARTICIPANT_ID> <INVESTMENTS> <INVESTMENT> <INVESTMENT_ID>ABC</INVESTMENT_ID> <BALANCE>1000</BALANCE> <ELECTION>0.00</ELECTION> </INVESTMENT> <INVESTMENT> <INVESTMENT_ID>XYZ</INVESTMENT_ID> <BALANCE>2000</BALANCE> <ELECTION>0.00</ELECTION> </INVESTMENT> <INVESTMENT> <INVESTMENT_ID>QWERTY</INVESTMENT_ID> <BALANCE>3000<

How can I group/sum values in LINQ to XML?

回眸只為那壹抹淺笑 提交于 2021-02-10 14:23:04
问题 I've been trying to work thru this but I am having problems grouping the data. I have an XML structure <FILE> <PLAN> <PLAN_ID>001</PLAN_ID> <PARTICIPANT> <PARTICIPANT_ID>9999</PARTICIPANT_ID> <INVESTMENTS> <INVESTMENT> <INVESTMENT_ID>ABC</INVESTMENT_ID> <BALANCE>1000</BALANCE> <ELECTION>0.00</ELECTION> </INVESTMENT> <INVESTMENT> <INVESTMENT_ID>XYZ</INVESTMENT_ID> <BALANCE>2000</BALANCE> <ELECTION>0.00</ELECTION> </INVESTMENT> <INVESTMENT> <INVESTMENT_ID>QWERTY</INVESTMENT_ID> <BALANCE>3000<

Create generic selector for Select() while using Entity Framework

半世苍凉 提交于 2021-02-10 13:25:10
问题 I would like to create a function to retrieve a List of a given property name's Type. But i dont know yet how to create a working lambda selector. public IList<object> GetDistinctListOfProperty(string propertyName) { var propInfoByName = typeof(T).GetProperty(propertyName); if (propInfoByName == null) return new List<object>(); using (var db = new ApplicationDbContext()) { var lambda = //TODO return db.Set<T>().Select(lambda).Distinct().ToList(); } } 回答1: You can use this method to generate

LINQ how to return last date and difference between first and last count

寵の児 提交于 2021-02-10 06:16:25
问题 I have a table with the following column and sample data: acteename updated_at count (count is not sorted in db) dev-52 2/7/2020 5:56:43 PM 1 dev-52 2/7/2020 5:56:52 PM 2 dev-52 2/7/2020 5:57:00 PM 3 dev-52 2/7/2020 5:57:49 PM 4 dev-52 2/7/2020 5:59:19 PM 5 dev-52 2/7/2020 6:01:51 PM 6 dev-52 2/7/2020 6:06:21 PM 7 dev-52 2/7/2020 6:14:51 PM 8 dev-52 2/7/2020 6:31:23 PM 9 dev-52 2/7/2020 6:47:54 PM 10 dev-52 2/7/2020 7:04:26 PM 11 dev-52 2/7/2020 7:20:58 PM 12 I want to return diff between

Entity Framework DBContext 增删改查深度解析

左心房为你撑大大i 提交于 2021-02-10 05:23:33
   Entity Framework DBContext 增删改查深度解析 有一段时间没有更新博客了,赶上今天外面下雨,而且没人约球,打算把最近对Entity Framework DBContext使用的心得梳理一下,早些时候在网上简单查过,对于最新版本的EF并没有类似的知识梳理类文章,希望对大家有所帮助。 1. 不要Code first, 也不要DB first 我为什么讨厌Code first和DB first呢?首先Code first是先写代码,数据库完全由代码生成,开发阶段尚可,一旦到了产品发布阶段,如果需要添加字段,我们总不能用 visual studio去生产环境上去更新数据库吧,听起来就很可怕。而且另外的一个问题自动是生成的数据库脚本也不可控,还不如自己提前设计好。DB first也好不了哪去,反向转过来的代码包含很多没有用的文件,而且数据库的更新还要重新走Model生成过程,简直无法理解为什么会有这样的设计。 说了这么多,怎么解决呢? 数据库和领域模型分开设计,按照对应关系映射字段,使用自定义链接字串,既不使用领域模型生成数据库,也不用数据库生成领域模型,示例代码如下, SQL Code 以 Destinations和TTable表为例: CREATE TABLE [DBO].[Destinations] ( [DestinationId] [ int ]

Group by two properties as one in Linq

断了今生、忘了曾经 提交于 2021-02-09 12:38:09
问题 I have the following class public class Booking { public string Group { get; set; } public BookingType Type { get; set; } public BookingStatus Status { get; set; } public InvoiceFreq { get; set; } public InvoiceLevel { get; set; } } That I'd need to group them by all properties in Linq so that the last two properties form one new field in the grouped result. Like this: Group Type Status InvoiceType ------------------------------------------- Group1 Online New Daily-Single Group2 Phone

Group by two properties as one in Linq

若如初见. 提交于 2021-02-09 12:37:05
问题 I have the following class public class Booking { public string Group { get; set; } public BookingType Type { get; set; } public BookingStatus Status { get; set; } public InvoiceFreq { get; set; } public InvoiceLevel { get; set; } } That I'd need to group them by all properties in Linq so that the last two properties form one new field in the grouped result. Like this: Group Type Status InvoiceType ------------------------------------------- Group1 Online New Daily-Single Group2 Phone