.net-3.5

How to sort a collection based on type in LINQ

試著忘記壹切 提交于 2020-08-27 05:22:06
问题 I have a collection like this, Class Base{} Class A : Base {} Class B : Base {} List<Base> collection = new List<Base>(); collection.Add(new A()); collection.Add(new B()); collection.Add(new A()); collection.Add(new A()); collection.Add(new B()); Now I want to sort the collection based on type (A/B). How I can do this? Please help me. 回答1: You can use the type information itself: collection.Sort( (a,b) => { bool aType = a.GetType() == typeof(A); bool bType = b.GetType() == typeof(A); return

LINQ performance

时光怂恿深爱的人放手 提交于 2020-02-29 06:50:06
问题 I am reading records from database and check some conditions and store in List<Result> . Result is a class. Then performing LINQ query in List<Result> like grouping, counting etc. So there may be chance that min 50,000 records in List<Result> , so in this whether its better to go for LINQ (or) reinsert the records to db and perform the queries? 回答1: Why not store it in an IQueryable instead of a List and using LINQ to SQL or LINQ to Entities, the actual dataset will never be pulled into

Conditional Linq Queries

纵然是瞬间 提交于 2020-02-08 19:26:34
问题 We're working on a Log Viewer. The use will have the option to filter by user, severity, etc. In the Sql days I'd add to the query string, but I want to do it with Linq. How can I conditionally add where-clauses? 回答1: if you want to only filter if certain criteria is passed, do something like this var logs = from log in context.Logs select log; if (filterBySeverity) logs = logs.Where(p => p.Severity == severity); if (filterByUser) logs = logs.Where(p => p.User == user); Doing so this way will

Conditional Linq Queries

时光毁灭记忆、已成空白 提交于 2020-02-08 19:24:26
问题 We're working on a Log Viewer. The use will have the option to filter by user, severity, etc. In the Sql days I'd add to the query string, but I want to do it with Linq. How can I conditionally add where-clauses? 回答1: if you want to only filter if certain criteria is passed, do something like this var logs = from log in context.Logs select log; if (filterBySeverity) logs = logs.Where(p => p.Severity == severity); if (filterByUser) logs = logs.Where(p => p.User == user); Doing so this way will

Understanding Lambda expressions and delegates [closed]

自闭症网瘾萝莉.ら 提交于 2020-01-30 14:36:19
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I have been trying to figure this out for quite sometime (reading online blogs and articlaes), but so far unsuccessful. What are delegates? What are Lambda Expressions? Advantages and disadvantages of both? Possible best practice of when to use one or the other? Thanks in advance.

Renaming of class property that is marked with a DataMemberAttribute

老子叫甜甜 提交于 2020-01-30 07:53:07
问题 Actually i have a class like this [DataContract] public class Item : IExtensibleDataObject { [Browsable(false)] public virtual ExtensionDataObject ExtensionData { get; set; } [DataMember] public string ID { get; set; } [DataMember] public string Name { get; set; } public Item() : this(String.Empty, String.Empty) { } public Item(string id, string name) { ID = id ?? String.Empty; Name = name ?? String.Empty; } } You can easily serialize and deserialize it. But now comes the hard part: I have to