Tag: linq

有没有办法覆盖LINQtoSQL生成的类中的空构造函数?

如果我的数据库中有一个名为’Users’的表,那么LINQtoSQL将生成一个名为’User’的类,其中已经声明了一个空构造函数。 如果我想覆盖这个构造函数并添加我自己的逻辑,那么最佳实践是什么?

使用LINQ从C#中的字符串中获取首字母缩略词?

下面是我如何编写一个函数来创建Java风格的首字母缩略词: string makeAcronym(string str) { string result = “”; for (int i = 0; i < str.Length; i++) { if (i == 0 && str[i].ToString() != " ") { result += str[i]; continue; } if (str[i – 1].ToString() == " " && str[i].ToString() != " ") { result += str[i]; } } return result; } […]

LINQ获取不同的值并填写LIST

我试图弄清楚我是否可以使用LINQ为我提供DataTable(FirstName,LastName,QTY)中的一些数据的不同值。 我可以得到不同的值并填充我的列表,但我必须运行两个不同的LINQ查询来获取它….我相信有更好的方法来做到这一点:) 任何建议将不胜感激(对LINQ来说很新) 码: public static List LinqDistinct(DataTable dt) { DataTable linqTable = dt; //get the distinct values var query = (from names in dt.AsEnumerable() select new { FirstName = names.Field(“FirstName”), LastName = names.Field(“LastName”) }).Distinct(); //fill my list with the distinct values List sList = (from sa in query.AsEnumerable() select new StudentData { FirstName = […]

如何使用LINQ实现左排除JOIN?

如何使用LINQ实现左排除JOIN? 在SQL中 : SELECT FROM Table_A A LEFT JOIN Table_B B ON A.Key = B.Key WHERE B.Key IS NULL

LEFT OUTER JOIN 2个数据表

我试图了解如何查询,最好使用LINQ,2个数据表。 我想对他们进行LEFT OUTER JOIN 带col的Datatable1:[ID] [colA] 带有col的DataTable2:[ID] [ColB] [ColC] … 希望加入该ID。 有人可以给我看一个例子,以便我可以将它应用到我的数据表吗? 先感谢您

automapper缺少类型映射配置或不支持的映射。

错误 Missing type map configuration or unsupported mapping. Mapping types: Cities_C391BA93C06F35100522AFBFA8F6BF3823972C9E97D5A49783829A4E90A03F00 -> IEnumerable`1 System.Data.Entity.DynamicProxies.Cities_C391BA93C06F35100522AFBFA8F6BF3823972C9E97D5A49783829A4E90A03F00 -> System.Collections.Generic.IEnumerable`1[[OsosPlus2.Core.DataAccess.Cities, OsosPlus2.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Destination path: CustomerViewModel.Cities.Cities Source value: System.Data.Entity.DynamicProxies.Cities_C391BA93C06F35100522AFBFA8F6BF3823972C9E97D5A49783829A4E90A03F00 行动方法: public ActionResult _EditCustomer(int CustomerId) { Customers customer = entity.Customers.FirstOrDefault(x => x.sno == CustomerId); CustomerViewModel customerViewModel = new CustomerViewModel(); customerViewModel = AutoMapper.Mapper.Map(customer); customerViewModel.Sectors = entity.Sectors; customerViewModel.Cities = entity.Cities; customerViewModel.PowerSuppliers […]

带有语句体的lambda表达式无法转换为nopCommerce 中的表达式树

我尝试在nopCommerce 3.0中创建一个linq连接查询。 我加入linq的两张桌子并写 代码成功了。 但视觉工作室的知识分子显示出错误 具有语句主体的lambda表达式无法转换为表达式树 请看下面的代码 var roles = _customerEventRoleRepository.Table.Where(c => c.EventId == selevent) .Join ( _customerRepository.Table, cev => cev.CustomerId, c => c.Id, (cev, c) => { var cust = new CustomerEventRolesModel(); cust.Id = cev.Id; cust.CustomerId = c.Id; cust.Customer = c.Email; cust.ContactName = c.GetAttribute(SystemCustomerAttributeNames.FirstName); cust.CompanyName = c.GetAttribute(SystemCustomerAttributeNames.Company); cust.Speaker = cev.IsSpeaker; cust.Sponsor = cev.IsSponser; return […]

Linq To SQL:按任意属性(列)名称排序查询

我有一个更大/更复杂的问题,但为了简单起见,让我们考虑以下内容: 我们假设我在SQL DataBase中有一个名为Product的表 ,它有两列, ID (int,主键)和Name (varchar / string) 。 我还有一个简单的LINQ DataContext 。 我有一个查询构造并交给“我的”function。 让我们说它是这样的:( 虽然它可能有点复杂) IQueryable query = from p in db.Products select p; 一旦我的方法获得此查询,作为参数传入,它必须更改排序顺序,例如 IQueryable sortedQuery = query.OrderBy(x => x.Name); 我想使这更通用,即指定要排序的字段。 通常,我可以执行一个带字符串的switch语句。 但是我想知道是否有办法直接传递参数。 我打算将它扩展到其他数据库表,因此这些switch语句会变得乏味。 我在尝试这样的事情: IQueryable sortedQuery = query.OrderBy(x => (typeof(Product)).GetProperty(“Name”)); 但这不起作用。 我还想确保维护LINQ to SQL,即在SQL Server上进行排序。 因此,如果我调试,我应该从这个LINQ查询获得一个SQL查询。 预先感谢您的帮助。

在C#中获取列表中的公共元素

我有两个排序列表如下: var list1 = new List() { 1, 1, 1, 2, 3 }; var list2 = new List() { 1, 1, 2, 2, 4 }; 我希望输出为: {1, 1, 2} 如何在C#中做到这一点? 有没有办法使用Linq?

如何将Foreach语句转换为linq表达式?

如何将foreach转换为linq表达式? var list = new List(); foreach (var id in ids) { list.Add(new Book{Id=id}); }