Tag: linq

使用LINQ连接两个表

我有两张桌子: PlanMaster(PlanName,Product_ID) 和 ProductPoints(Entity_ID,Product_ID,Comm1,Comm2) 现在我将Entity_ID存储到一个存储在’int’中的Session中: int getEntity = Int16.Parse(Session[“EntitySelected”].ToString()); 我想在我的LINQ查询中显示上面表格中的所有项目 Entity_ID = getEntity 这是我的LINQ查询: var td = from s in cv.Entity_Product_Points join r in dt.PlanMasters on s.Product_ID equals r.Product_ID where s.Entity_ID = getEntity select s; 现在它给我一个错误,上面写着: 无法隐式转换类型’int?’ ‘bool’ 这里出了什么问题? 感谢您提前的意见!

如何在Linq中使用Long类型跳过

如何在Linq中使用Skip的long类型(Int64)。 它仅支持Int32。 dataContext.Persons.Skip(LongNumber);

通过LINQ递归选择?

可能重复: linq to sql递归查询 我不得不通过LINQ为自引用表构建一个递归选择。 我用这个class: public class DivisionHierarchy { public Division Division { get; set; } public IEnumerable Divisions { get; set; } } 我创建了这个函数但不知何故它是无限的。 public IEnumerable GetDivisionHierarchy(IEnumerable allDivisions, Division parentDivision) { Guid? parentDivisionId = null; if (parentDivision != null) parentDivisionId = parentDivision.DivisionID; var childDivisions = allDivisions.Where(e => e.DivisionID == parentDivisionId); Collection hierarchy = […]

TransactionScope中的Membership.GetUser()抛出TransactionPromotionException

以下代码抛出TransactionAbortedException ,消息“事务已中止”,内部TransactionPromotionException ,消息“尝试提升事务时失败”: using ( TransactionScope transactionScope = new TransactionScope() ) { try { using ( MyDataContext context = new MyDataContext() ) { Guid accountID = new Guid( Request.QueryString[ “aid” ] ); Account account = ( from a in context.Accounts where a.UniqueID.Equals( accountID ) select a ).SingleOrDefault(); IQueryable loginList = from l in context.Logins where […]

从自引用数据库表中填充递归数据结构

这个问题涉及http://www.matthidinger.com/archive/2009/02/08/asp.net-mvc-recursive-treeview-helper.aspx 假设我有一个看起来像这样的表: alt text http://sofzh.miximages.com/c%23/image_3.png 我有一个递归数据结构,如下所示: public class TreeNode { public TreeNode(){} public string NodeId { get; set; } public string ParentId { get; set; } public string Name { get; set; } public IEnumerable Children { get; } } 如何使用Linq从表中填充此递归数据结构? 注意:出于这个问题的目的,请假设我已经有一个非常有效的表格; 即它完全驻留在内存中,或者使用CTE进行访问。 真的,我只是在寻找Linq查询,以便从Linq到SQL DataContext到递归对象。 我知道它可能涉及一个ForEach和一个递归函数调用; 我只是无法理解它。 提前致谢。

如何使用C#在.Net中的类型对象列表中选择对象属性的所有值

呃,我怎么解释这个…可能是一个简单的问题,但我的思绪是炒的。 假设我有这个课程: public class NestedObject { public string NestedName { get; set; } public int NestedIntValue { get; set; } public decimal NestedDecimalValue { get; set; } } public class SomeBigExternalDTO { public int Id { get; set; } public int UserId { get; set; } public int SomeIntValue { get; set; } public long […]

获取List 中的最高可用数量

我在List有以下字符串 vs0,vs1,vs2,vs3,vs4,vs5,… vs(n) 在我的列表中,它们没有排序并且是随机的。 我想获得其中包含最高int的字符串。 然后将该字符串中的数字转换为int var。 这是最好最快的方法是什么?

LINQ Join查询(表之间有可为空的ref)

我有3张桌子。 例如客户 , 公司和地址 。 客户已获得公司的支持。 公司有2个可空的地址(账单和运输),因此在某些情况下可能不存在地址。 我需要make join查询,但是如果Company.BillingAddress或Company.ShippingAddress等于null ,我不会获得所有数据)。 我试过了(但这是错误的查询): var res = (from client in context.Clients join clientCompany in context.Companies on client.ClientCompanyId equals clientCompany.Id into clientCompanyJoin from company in clientCompanyJoin join addressBilling in context.Addresses on company.BillingAddressId equals addressBilling.Id join addressShipping in context.Addresses on company.ShippingAddressId equals addressShipping.Id select new { Client = client, Company […]

动态LINQ表达式

我正在尝试实现这个如何在Linq where子句中指定动态字段名称? 并得到一个编译器错误,说: 无法解析方法’Where(System.Linq.Expressions.LambdaExpression public class Employee { public string Name { get; private set; } public int Salary { get; private set; } public Employee(string name, int salary) { Name = name; Salary = salary; } } 然后在控制台应用程序的主要方法 var employees = new List { new Employee(“Bob”, 45000), new Employee(“Jane”, 25000), new Employee(“Jim”, 5) }; […]

从列表值c#动态创建匿名对象

我有一个列表(或可以是数组)的字符串,我想动态创建一个匿名对象。 我该怎么做呢? var dataSet = new DataSet(); dataSet.ReadXml(@””); var dataTable = dataSet.Tables[0]; var dataRow = dataTable.Rows[0]; var keys = new List {“Column1″,”Column2”}; var result = new {keys[0] = dataRow[keys[0]], keys[1] = dataRow[keys[1]]} 因此,名为“keys”的列表将在此方法之外创建,并且可以包含1到多个值。 我尝试创建一个字典并循环遍历列表并向字典添加键/值对,但后来我无法弄清楚如何将字典转换回匿名类型。 我也尝试过expando对象,但这似乎并没有让我更进一步。 我必须能够返回一个匿名类型,因为此方法的结果将与LINQ查询的GroupBy子句一起使用。 这是我必须动态创建字典的方法: public object Key(DataRow dataRow, List keys) { var dictionary = new IDictionary; foreach (string key in keys) { […]