linq-to-objects

LINQ Except() Method Does Not Work

此生再无相见时 提交于 2019-12-23 12:10:40
问题 I have 2 IList<T> of the same type of object ItemsDTO . I want to exclude one list from another. However this does not seem to be working for me and I was wondering why? IList<ItemsDTO> related = itemsbl.GetRelatedItems(); IList<ItemsDTO> relating = itemsbl.GetRelatingItems().Except(related).ToList(); I'm trying to remove items in related from the relating list. 回答1: Since class is a reference type, your ItemsDTO class must override Equals and GetHashCode for that to work. 回答2: From MSDN:

Iterating over class properties using LINQ

≯℡__Kan透↙ 提交于 2019-12-23 09:33:54
问题 There is a ParsedTemplate class that it has over 300 property (typed Details and BlockDetails). The parsedTemplate object will be fill by a function. After filling this object I need a LINQ (or other way) to find is there any property like "body" or "img" where IsExist=false and Priority="high" . public class Details { public bool IsExist { get; set; } public string Priority { get; set; } } public class BlockDetails : Details { public string Block { get; set; } } public class ParsedTemplate {

Return min value in group with lambda/linq query

*爱你&永不变心* 提交于 2019-12-22 05:45:09
问题 I need help creating a lambda expression to query the following list for retrieving the lowest priced item in each channel. Ie for this example item A, D and G class Radio { public string Name { get; set; } public int Channel { get; set; } public decimal Price { get; set; } } List<Radio> radios = new List<Radio>(); radios.Add(new Radio() { Name = "A", Channel = 1, Price = 10 }); radios.Add(new Radio() { Name = "B", Channel = 1, Price = 20 }); radios.Add(new Radio() { Name = "C", Channel = 1,

LINQ query with multiple aggregates

前提是你 提交于 2019-12-22 05:18:14
问题 How would I create the equivalent Linq To Objects query? SELECT MIN(CASE WHEN p.type = "In" THEN p.PunchTime ELSE NULL END ) AS EarliestIn, MAX(CASE WHEN p.type = "Out" THEN p.PunchTime ELSE NULL END ) AS LatestOUt FROM Punches p 回答1: You can't efficiently select multiple aggregates in vanilla LINQ to Objects. You can perform multiple queries, of course, but that may well be inefficient depending on your data source. I have a framework which copes with this which I call "Push LINQ" - it's

Using LINQ to objects Intersect and Except on a specific property

倖福魔咒の 提交于 2019-12-22 05:09:16
问题 When I have 2 List<string> objects, then I can use Intersect and Except on them directly to get an output IEnumerable<string> . That's simple enough, but what if I want the intersection/disjuction on something more complex? Example, trying to get a collection of ClassA objects which is the result of the intersect on ClassA object's AStr1 and ClassB object's BStr ; : public class ClassA { public string AStr1 { get; set; } public string AStr2 { get; set; } public int AInt { get; set; } } public

How to group ranges in LINQ

坚强是说给别人听的谎言 提交于 2019-12-21 06:26:55
问题 Here is the data that I would like to group. Start End 2 4 26 30 5 9 20 24 18 19 Because I have 18 - 19 and 20 - 24. I would add these two together as 18 - 24. In this case the rule is (a, b) => b.start - a.end = 1 and the result would be Start End 18 24 2 9 26 30 EDIT added last result row per comments below. 回答1: So we'll start with a helper method called GroupWhile . It will be provided with a predicate accepting two items from the sequence, the previous and the current. If that predicate

Handling temporary calculation in Linq

妖精的绣舞 提交于 2019-12-21 05:21:18
问题 When solving an interview question Question A six digit number need to be found in such a manner when it is multiplied by an integer between 2 and 9 gives the original six digit number when its digits are reversed. Example: Suppose I multiply 219978 * 4 i get 879912 ,when reverse 879912 I will get 219978 back. I solved it using for (long l = 100000; l < 999999; l++) { var num = l.ToString(); for (int i = 3; i < 9; i++) { var mul = l * i; string str = mul.ToString(); char[] splitDigits = str

IQueryable Lambda Projection Syntax

前提是你 提交于 2019-12-21 04:59:11
问题 I have an IQueryable whose Entity Framework 4 objects I would like to project to their DTO equivalents. One such object 'Person' is an EF4 class, and the corresponding POCO PersonP is a class I've defined. I am using Automapper to map between them. However, when I try the following code: IQueryable<Person> originalModel = _repo.QueryAll(); IQueryable<PersonP> projection = originalModel.Select(e => Mapper.Map<Person, PersonP>(e)); The projection generates this error at runtime: LINQ to

At least one object must implement IComparable

≡放荡痞女 提交于 2019-12-21 03:20:09
问题 var listair = empcon.OrderBy(x => x.CustomerConnection.OrderBy(y => y.Id)).ToList(); When I am using this statement then I am getting exception "At least one object must implement IComparable" How can I solve this problem? 回答1: I had this problem with my query when I wrote it wrong: IEnumerable<OrgRelation> relations = from r in tree.OrgRelations orderby r.ParentUnit, r.ChildUnit select r; This was because the Parent and Child Units are both OrgUnit objects the are related to this OrgRelation

How do I write a LINQ query that inverts the grouping of a hierarchical data source?

☆樱花仙子☆ 提交于 2019-12-21 02:41:36
问题 How would one write a LINQ query which takes a hierarchical source data and transforms it so that the grouping is inverted? Say I have a list of Topic objects each of which contains a collection of Tags which represent meta-data tags on that topic. What I need is to write a LINQ query to basically flip the hierarchy inside out so that I have a list of Tags each of which have a collection of topics that are tagged with that particular tag. Topic { Title = "Political Debate #1", Posted = 01/02