linq

Find common items in multiple lists in C# linq

北战南征 提交于 2020-12-13 05:41:01
问题 I searched, but I found only answers which related to two lists. But what about when they are more than two? List 1 = 1,2,3,4,5 List 2 = 6,7,8,9,1 List 3 = 3,6,9,2,0,1 List 4 = 1,2,9,0,5 List 5 = 1,7,8,6,5,4 List 6 = 1 List 7 = How to get the common items? as you can see one of them is empty, so the common will be empty, but I need to skip empty lists. 回答1: var data = new [] { new List<int> {1, 2, 3, 4, 5}, new List<int> {6, 7, 8, 9, 1}, new List<int> {3, 6, 9, 2, 0, 1}, new List<int> {1, 2,

Find common items in multiple lists in C# linq

℡╲_俬逩灬. 提交于 2020-12-13 05:40:26
问题 I searched, but I found only answers which related to two lists. But what about when they are more than two? List 1 = 1,2,3,4,5 List 2 = 6,7,8,9,1 List 3 = 3,6,9,2,0,1 List 4 = 1,2,9,0,5 List 5 = 1,7,8,6,5,4 List 6 = 1 List 7 = How to get the common items? as you can see one of them is empty, so the common will be empty, but I need to skip empty lists. 回答1: var data = new [] { new List<int> {1, 2, 3, 4, 5}, new List<int> {6, 7, 8, 9, 1}, new List<int> {3, 6, 9, 2, 0, 1}, new List<int> {1, 2,

OrderBy list by a nested list

浪子不回头ぞ 提交于 2020-12-12 11:09:13
问题 I have a objectA list, each one contain an another objectB list. I'm trying to OrderBy each objectB list with an (int) Id : var sorted = objectA.OrderBy(a => a.ObjectB.OrderBy(b => b.Id)).ToList(); Of course, this doesn't work. Someone have an advice ? 回答1: If you want to sort each ObjectB list in place (i.e. modify it), then simply use the List<T>.Sort method. You will need to specify a custom Comparison<T> delegate: foreach (var a in objectA) { a.ObjectB.Sort((x, y) => x.Id - y.Id) } If

OrderBy list by a nested list

与世无争的帅哥 提交于 2020-12-12 11:08:47
问题 I have a objectA list, each one contain an another objectB list. I'm trying to OrderBy each objectB list with an (int) Id : var sorted = objectA.OrderBy(a => a.ObjectB.OrderBy(b => b.Id)).ToList(); Of course, this doesn't work. Someone have an advice ? 回答1: If you want to sort each ObjectB list in place (i.e. modify it), then simply use the List<T>.Sort method. You will need to specify a custom Comparison<T> delegate: foreach (var a in objectA) { a.ObjectB.Sort((x, y) => x.Id - y.Id) } If

C# Linq Expression could not be Translated

别来无恙 提交于 2020-12-12 03:59:13
问题 I am trying to execute a linq query to get all employes that have some specific skills. search.skills is a list of strings with some skills and I want to retrieve all the user which have all those skills (And condition). In my where clause on employees, exp.Skills is ICollection and expSkill.SkillName is the skill name .Where( emp => search.Skills.All( searchSkill => emp.Experiences.Select(exp => exp.Skills).SelectMany(x => x).Select(expSkill => expSkill.SkillName).Contains(searchSkill) ))

C# Linq Expression could not be Translated

≡放荡痞女 提交于 2020-12-12 03:59:12
问题 I am trying to execute a linq query to get all employes that have some specific skills. search.skills is a list of strings with some skills and I want to retrieve all the user which have all those skills (And condition). In my where clause on employees, exp.Skills is ICollection and expSkill.SkillName is the skill name .Where( emp => search.Skills.All( searchSkill => emp.Experiences.Select(exp => exp.Skills).SelectMany(x => x).Select(expSkill => expSkill.SkillName).Contains(searchSkill) ))

Linq-To-Entities using method to select new object

早过忘川 提交于 2020-12-09 08:05:50
问题 I try to use the same select query multiple times in my application. For example I have this select statement: _db.tbl_itembundlecontents .Select(z => new SingleItemDTO { amount = z.amount, id = z.id, position = z.position, contentType = new BasicMaterialDTO { id = z.tbl_items.id, img = z.tbl_items.tbl_material.img, name = z.tbl_items.tbl_material.name, namekey = z.tbl_items.tbl_material.namekey, info = z.tbl_items.tbl_material.info, weight = z.tbl_items.tbl_material.weight } }); but i also

Linq-To-Entities using method to select new object

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-09 08:03:32
问题 I try to use the same select query multiple times in my application. For example I have this select statement: _db.tbl_itembundlecontents .Select(z => new SingleItemDTO { amount = z.amount, id = z.id, position = z.position, contentType = new BasicMaterialDTO { id = z.tbl_items.id, img = z.tbl_items.tbl_material.img, name = z.tbl_items.tbl_material.name, namekey = z.tbl_items.tbl_material.namekey, info = z.tbl_items.tbl_material.info, weight = z.tbl_items.tbl_material.weight } }); but i also

AmbiguousMatchException in Expression.PropertyOrField

喜欢而已 提交于 2020-12-09 04:26:07
问题 I am using reflection to create a lambda function. It works with most items I try it with, however on one of the properties it keeps throwing an Ambiguous Match Exception. The code looks like this. The error happens when it hits Expression.PropertyOrField . The property I am using is of type decimal? . I think it might have to do with the fact that it is a nullable type, but I'm not sure. public static LambdaExpression CreateExpression(Type type, string propertyName, ref Type returnType) {

AmbiguousMatchException in Expression.PropertyOrField

感情迁移 提交于 2020-12-09 04:24:03
问题 I am using reflection to create a lambda function. It works with most items I try it with, however on one of the properties it keeps throwing an Ambiguous Match Exception. The code looks like this. The error happens when it hits Expression.PropertyOrField . The property I am using is of type decimal? . I think it might have to do with the fact that it is a nullable type, but I'm not sure. public static LambdaExpression CreateExpression(Type type, string propertyName, ref Type returnType) {