linq

Join 2 lists have different length by in LINQ

こ雲淡風輕ζ 提交于 2021-02-05 11:13:42
问题 How can I join 2 lists of different lengths. it should join with the sequence. Eg. {1,2,3,4} with {5,6,7} I need to get result like below. {{1,5}, {2,6}, {3,7}, {4,null}} I tried this. var qry = a.Select((i, index) => new {i, j = b[index]}); But its throwing error since the lists are having different lengths. Please help me to get the solution. 回答1: This should work: var a = new int?[] { 1, 2, 3, 4 }; var b = new int?[] { 5, 6, 7 }; var result = Enumerable.Range(0, Math.Max(a.Count(), b.Count

Join 2 lists have different length by in LINQ

自作多情 提交于 2021-02-05 11:13:24
问题 How can I join 2 lists of different lengths. it should join with the sequence. Eg. {1,2,3,4} with {5,6,7} I need to get result like below. {{1,5}, {2,6}, {3,7}, {4,null}} I tried this. var qry = a.Select((i, index) => new {i, j = b[index]}); But its throwing error since the lists are having different lengths. Please help me to get the solution. 回答1: This should work: var a = new int?[] { 1, 2, 3, 4 }; var b = new int?[] { 5, 6, 7 }; var result = Enumerable.Range(0, Math.Max(a.Count(), b.Count

Filtering a list of processes based on a list of string objects

余生颓废 提交于 2021-02-05 10:48:43
问题 I have a List of string objects which represent process names. I want to filter a collection of running Processes (retrieved by using GetProcesses()), by using the list of string objects I have. So if I want to look for the Sql Server process running in the processes collection, I will look for the string name as stored in the string list. How can I filter a list of processes to get only the processes which have the same process names as a list of strings (the different generic types makes it

How to remove elements of a list where its string contains sub strings from another list

依然范特西╮ 提交于 2021-02-05 10:46:23
问题 I have a string like this: var str = "DAVID CORPORATION" then i have a list of substrings that i dont want in the str. var describers = new List<string> {"CORP", "INC", "LTD", "TECH", "ENGINEER", "LLC","MICROELE"}; then i split the str here into a list: var strList = str.Split(' ').ToList(); now i want to remove all items of that list that contains the substrings in describers. I found this way to do it a million times all over the internet. strList.RemoveAll(x => describers.Contains(x));

copy chosen properties to object of other type

梦想与她 提交于 2021-02-05 10:38:50
问题 I need to copy properties of one object to another, both objects can be of different type, can have properties of same name. These property can also be of complex type. I was able to achieve the copy feature for simple TYPE properties, how ever i am unable to achieve this for complex types.. like see below sample snippet [TestClass] public class PropAssignTest { [TestMethod] public void Test() { Test1 t1 = new Test1() { Prop1 = "test", TestName = new Test3 { Name = "santosh" } } ; Test2 t2 =

Order by list in complex object

此生再无相见时 提交于 2021-02-05 10:01:47
问题 I have two classes : public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public List<Product> Product { get; set; } } public class Product { public int ProductNumber { get; set; } public string ProductColor { get; set; } } And one instance : Customer Cus = new Customer() { FirstName = "FirstName1", LastName = "LastName1", Product = new List<Product> { new Product() { ProductColor = "ProductColor12", ProductNumber = 12 }, new Product() {

Retrieving binary back to image and from database and save into a folder using WPF

ε祈祈猫儿з 提交于 2021-02-05 09:48:21
问题 I have successfully converted image to binary and saved it into the database using linq to sql WPF and now i want to retrieve it back to image format and save it to a specific folder in computer. i have read many blogs and articles which retrieves the image binary from database and then shows it into the PictureBox, what i want to do is to select the image and save it to a specific folder using linq to sql. code which i have tried so far for uploading an image: private void Browse_Click

Retrieving binary back to image and from database and save into a folder using WPF

夙愿已清 提交于 2021-02-05 09:48:08
问题 I have successfully converted image to binary and saved it into the database using linq to sql WPF and now i want to retrieve it back to image format and save it to a specific folder in computer. i have read many blogs and articles which retrieves the image binary from database and then shows it into the PictureBox, what i want to do is to select the image and save it to a specific folder using linq to sql. code which i have tried so far for uploading an image: private void Browse_Click

EF Core Query Many to Many with filtering

∥☆過路亽.° 提交于 2021-02-05 09:31:30
问题 I have the following table structure: I want to retrieve all funds for provided reportId . I did it this way: var result = _context.FundsInReports .Join(_context.Funds, a=> a.FundId, b => b.Id, (fir, fund) => new {fir, fund}) .Join(_context.Reports, a=> a.fir.ReportId, b=> b.Id, (fir2, report) => new { fir2, report}) .Where(q=> q.fir2.fir.ReportId==reportId) .Select(res => new FundsResponse() { FundId = res.fir2.fund.Id, LegalName = res.fir2.fund.LegalName, HeaderName = res.fir2.fund

How Order by Case in LINQ

て烟熏妆下的殇ゞ 提交于 2021-02-05 08:41:53
问题 I have this answer Entity framework OrderBy "CASE WHEN" but this only handle two options var p = ctx.People.OrderBy(p => (p.IsQualityNetwork == 1 || p.IsEmployee == 1) ? 0 : 1) .ThenBy(p => p.Name); I have a text field and want rows appear on an specific order. In sql I would do: ORDER BY CASE when name = "ca" then 1 when name = "po" then 2 when name = "pe" then 3 when name = "ps" then 4 when name = "st" then 5 when name = "mu" then 6 END 回答1: How about creating a map of your sort order? var