[Solved] LINQ to XML attributes

You can do it this way: var result= xdoc.Descendants(“image”) .Where(x => x.Attribute(“size”).Value == “large”) .Select(x => new User{ Image = x.Value }); Here is Working Example Fiddle solved LINQ to XML attributes

[Solved] How to filter data based on maximum datetime value in each day [closed]

With data like: public class Data { public String Name { get; set; } public DateTime TimeStamp { get; set; } public Data(String name, DateTime timeStamp) { this.Name = name; this.TimeStamp = timeStamp; } public override string ToString() { return String.Format(“{0} at {1}”, this.Name, this.TimeStamp); } } You can use: var source = new Data[] … Read more

[Solved] Printing Month Name in Linq

Rather than using ToString, try string.Format. Something like: var query = (from e in Employees let month = e.BirthDate.GetValueOrDefault() let birthmonth = string.Format(“{0:MMMM}”, month) select birthmonth); query.Dump(); This seems to work from my local testing, although it is not included as part of the SQL query. 0 solved Printing Month Name in Linq

[Solved] How to resolve InvalidCastException after translating LINQ-to-JSON query from c# to VB.NET?

In order for {columns}.Concat(rows) to work, it seems you need to add an explicit call to AsEnumerable() in order to make sure the type TSource for Enumerable.Concat(IEnumerable<TSource>, IEnumerable<TSource>) is inferred correctly: Dim csvRows = { columns.AsEnumerable() }.Concat(rows) _ .Select(Function(r) String.Join(“,”, r)) Fixed fiddle #1 here. A DirectCast({columns}, IEnumerable(Of IEnumerable(Of String))) also seems to work, as … Read more

[Solved] Razor Page .NET Core 2.2 If + ElseIf statement doesn’t work in Lambda expression [duplicate]

I replaced this: .OrderBy(p => { if (p.Office == “President”) return 0; else if (p.Office == “Vice-President”) return 1; else if (p.Office == “Secretary-Treasurer”) return 2; }).ToListAsync(); with this: .OrderBy(p => p.Office == “President” ? 0 : p.Office == “Vice-President” ? 1 : p.Office == “Secretary-Treasurer” ? 2 : 3).ToListAsync(); I’m hoping it is useful … Read more

[Solved] Replace a string in string that is in a List [closed]

Simple. Iterate over all strings inside List and then check if myString contains that or not. If yes, then replace it. foreach (string item in lst) { if (myString.Contains(item)) { myString = myString.Replace(item, string.Format(“$${0}$$”, item)); } } Also you can do same with LINQ: lst.Where(item=> myString.Contains(item)).ToList() .ForEach(item => myString = myString.Replace(item, string.Format(“$${0}$$”, item))); You can … Read more

[Solved] Group by a nested list of object based on date [closed]

var temp = dbconnect.tblAnswerLists .Where(i => i.StudentNum == studentNumber && i.username==objstu.Return_SupervisorUserName_By_StudentNumber(studentNumber)) .ToList() // <– This will bring the data into memory. .Select(i => new PresentClass.supervisorAnswerQuesttionPres { answerList = Return_Answer_List(studentNumber,i.dateOfAnswer.Value.Date), questionList = Return_Question_List(studentNumber, i.dateOfAnswer.Value.Date), date = ConvertToPersianToShow(i.dateOfAnswer.Value.Date) }) .GroupBy(i => i.date) .OrderByDescending(i => i.Key) .ToList(); temp will actually be of type List<IGrouping<string, PresentClass.supervisorAnswerQuesttionPres>>. 2 solved Group … Read more

[Solved] C# Lookup ptimisation suggestion are welcome

Neater, more linq: var data = GetData(); foreach (var v0 in data) { if (v0.Key.Item3 != string.Empty) continue; //Get all related data var tr_line = data[v0.Key]; sb.AppendLine(tr_line.First()); var hhLines = from v1 in data where v1.Key.Item2 == string.Empty && v1.Key.Item1 == v0.Key.Item1 select data[v1.Key]; foreach (var hh_line in hhLines) { sb.AppendLine(hh_line.First()); var grouping = v0; … Read more

[Solved] How to correctly add Func to Linq?

Reducing the code to its basic form, your first snippet is of the form list.Where(p => NameContainsPassword(p) || HasEncryptedConfigurationItemAttribute(p)) where I inlined your IsNeverSerializedProperty function. The second function is of the form list .Where(p => NameContainsPassword(p)) .Where(p => HasEncryptedConfigurationItemAttribute(p)) which first filters all items for which NameContainsPassword(p) holds and then from that filters all items … Read more