Tag: linq

如何在LINQ lambda中的多个表之间执行连接

我正尝试在LINQ中的多个表之间执行联接 。 我有以下类: Product {Id, ProdName, ProdQty} Category {Id, CatName} ProductCategory{ProdId, CatId} //association table 我使用下面的代码(其中product , category和productcategory是上述类的实例): var query = product.Join(productcategory, p => p.Id, pc => pc.ProdID, (p, pc) => new {product = p, productcategory = pc}) .Join(category, ppc => ppc.productcategory.CatId, c => c.Id, (ppc, c) => new { productproductcategory = ppc, category = c}); […]

如何使用LINQ获取索引?

给定一个这样的数据源: var c = new Car[] { new Car{ Color="Blue", Price=28000}, new Car{ Color="Red", Price=54000}, new Car{ Color="Pink", Price=9999}, // .. }; 我怎样才能find满足一定条件的第一辆汽车的指数 ? 编辑: 我可以想到这样的事情,但看起来很可怕: int firstItem = someItems.Select((item, index) => new { ItemName = item.Color, Position = index }).Where(i => i.ItemName == "purple") .First() .Position; 用一个普通的旧循环来解决这个问题是否是最好的?

使用LINQ获取一个List <>中的项目,这些项目不在另一个List <>中

我会假设有一个简单的LINQ查询来做到这一点,我只是不完全知道如何。 请参阅下面的代码片段,注释说明了我想要做的事情: class Program { static void Main(string[] args) { List<Person> peopleList1 = new List<Person>(); peopleList1.Add(new Person() { ID = 1 }); peopleList1.Add(new Person() { ID = 2 }); peopleList1.Add(new Person() { ID = 3 }); List<Person> peopleList2 = new List<Person>(); peopleList2.Add(new Person() { ID = 1 }); peopleList2.Add(new Person() { ID = 2 }); […]

在Linq OrderBy中使用自己的IComparer <T>

我有一个通用的 List<MyClass> 其中MyClass有一个属性InvoiceNumber ,它包含如下的值: 一分之二十零万零九百零六 二分之二十零万零九百零六 .. 十分之二十零万〇九百〇六 十一分之二十零万零九百零六 十二分之二十万○九百○六 我的清单是绑定到一个 BindingList<T> 它支持用linq进行sorting: protected override void ApplySortCore( PropertyDescriptor property, ListSortDirection direction) { _sortProperty = property; _sortDirection = direction; var items = this.Items; switch (direction) { case ListSortDirection.Ascending: items = items.OrderByDescending(x => property.GetValue(x)).ToList(); break; case ListSortDirection.Descending: items = items.OrderByDescending(x => property.GetValue(x)).ToList(); break; } this.Items = items; […]

LINQ to Entities不识别方法System.String Format(System.String,System.Object,System.Object)'

我有这个linq查询: private void GetReceivedInvoiceTasks(User user, List<Task> tasks) { var areaIds = user.Areas.Select(x => x.AreaId).ToArray(); var taskList = from i in _db.Invoices join a in _db.Areas on i.AreaId equals a.AreaId where i.Status == InvoiceStatuses.Received && areaIds.Contains(a.AreaId) select new Task { LinkText = string.Format(Invoice {0} has been received from {1}, i.InvoiceNumber, i.Organisation.Name), Link = Views.Edit }; } […]

为什么LINQ to Entities不能识别方法System.String ToString()?

在MVC3 Web应用程序中获取错误。 LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. 当我尝试从查询中使用EF获取值: public class DataRepository { public mydataEntities1 dbContext = new mydataEntities1(); public List<SelectListItem> GetPricingSecurityID() { var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing select new SelectListItem { Text = m.PricingSecurityID.ToString(), Value = m.PricingSecurityID.ToString() […]

不区分大小写的string比较在LINQ到SQL中

我读过使用ToUpper和ToLower来执行不区分大小写的string比较是不明智的,但是当涉及到LINQ-to-SQL时,我看不到任何其他select。 String.Compare的ignoreCase和CompareOptions参数被LINQ-to-SQL忽略(如果您使用的是区分大小写的数据库,即使您要求区分大小写的比较,也可以区分大小写)。 ToLower或ToUpper在这里最好的select? 这个比那个好吗? 我以为我在某个地方看过ToUpper比较好,但我不知道这里是否适用。 (我正在做很多代码评论,每个人都在使用ToLower。) Dim s = From row In context.Table Where String.Compare(row.Name, "test", StringComparison.InvariantCultureIgnoreCase) = 0 这转换为一个SQL查询,它简单地将row.Name与“test”进行比较,并且不会在区分大小写的数据库上返回“Test”和“TEST”。

我如何使用LINQ Contains(string )而不是Contains(string)

我有一个很大的问题。 我有一个linq查询来把它看起来像这样: from xx in table where xx.uid.ToString().Contains(string[]) select xx string[]数组的值将是数字(1,45,20,10等) .Contains的默认值是.Contains(string) 。 我需要它来做这个,而不是: .Contains(string[]) … 编辑:一个用户build议写string[]的扩展类。 我想学习如何,但是任何人都愿意把我指向正确的方向? 编辑: uid也是一个数字。 这就是为什么它被转换成一个string。 帮助任何人?

仅在执行时调用types参数的generics方法

编辑: 当然,我真正的代码看起来不像这样。 我试图编写半伪代码,使其更清楚我想做的事情。 看起来只是把事情搞砸了。 所以,我真正想要做的是这样的: Method<Interface1>(); Method<Interface2>(); Method<Interface3>(); … 那么…我想也许我可以把它变成一个循环使用reflection。 所以问题是:我该怎么做。 我对反思的知识很浅。 所以代码示例会很好。 该scheme如下所示: public void Method<T>() where T : class {} public void AnotherMethod() { Assembly assembly = Assembly.GetExecutingAssembly(); var interfaces = from i in assembly.GetTypes() where i.Namespace == "MyNamespace.Interface" // only interfaces stored here select i; foreach(var i in interfaces) { Method<i>(); // […]

ToList() – 它是否创build一个新的列表?

假设我有一堂课 public class MyObject { public int SimpleInt{get;set;} } 我有一个List<MyObject>和我ToList()它,然后改变其中一个SimpleInt ,我的变化将传播回到原来的列表。 换句话说,以下方法的输出是什么? public void RunChangeList() { var objs = new List<MyObject>(){new MyObject(){SimpleInt=0}}; var whatInt = ChangeToList(objs ); } public int ChangeToList(List<MyObject> objects) { var objectList = objects.ToList(); objectList[0].SimpleInt=5; return objects[0].SimpleInt; } 为什么? P / S:如果看起来很明显,我很抱歉。 但我现在没有编译器