linq-query-syntax

Does LINQ “Query Syntax” Support Duck Typing?

余生颓废 提交于 2019-12-01 14:13:08
Regarding LINQ query syntax... var foo = new List<int> { 1, 2 }; var boo = from n in foo where n > 1 select n; ...I always thought this syntax was limited to operating on IEnumerable . Or at least until I learned about IQueryable. And perhaps IObservable as well. But I recently noticed a suggestion that query syntax is based on duck typing . That story didn't look terribly convincing, until I found a site that is dedicated to LINQ to Tasks . LINQ to Tasks looks like it is wholly dependent on duck typing with query syntax! Ok, what is going on here? Is query syntax using duck typing or not?

Return list of specific property of object using linq

倾然丶 夕夏残阳落幕 提交于 2019-11-30 03:06:29
Given a class like this: public class Stock { public Stock() {} public Guid StockID { get; set; } public string Description { get; set; } } Lets say I now have a List<Stock> . If I would like to retrieve a list of all the StockIDs and populate it into a IEnumerable or IList. Obviously I can do this. List<Stock> stockItems = new List<Stock>(); List<Guid> ids = new List<Guid>(); foreach (Stock itm in stockItems) { ids.Add(itm.StockID); } But is there some way I could use Linq to achieve the same result? I thought Distinct() might do it but couldn't work out how to achieve the desired result. var

Return list of specific property of object using linq

两盒软妹~` 提交于 2019-11-29 00:18:14
问题 Given a class like this: public class Stock { public Stock() {} public Guid StockID { get; set; } public string Description { get; set; } } Lets say I now have a List<Stock> . If I would like to retrieve a list of all the StockIDs and populate it into a IEnumerable or IList. Obviously I can do this. List<Stock> stockItems = new List<Stock>(); List<Guid> ids = new List<Guid>(); foreach (Stock itm in stockItems) { ids.Add(itm.StockID); } But is there some way I could use Linq to achieve the

Parse string into a LINQ query

牧云@^-^@ 提交于 2019-11-27 13:28:28
问题 What method would be considered best practice for parsing a LINQ string into a query? Or in other words, what approach makes the most sense to convert: string query = @"from element in source where element.Property = ""param"" select element"; into IEnumerable<Element> = from element in source where element.Property = "param" select element; assuming that source refers to an IEnumerable<Element> or IQueryable<Element> in the local scope. 回答1: It requires some text parsing and heavy use of

Extension methods syntax vs query syntax

僤鯓⒐⒋嵵緔 提交于 2019-11-26 19:42:56
I'm trying to get a handle on if there's a good time to use standard linq keywords or linq extension methods with lambda expressions. They seems to do the same thing, just are written differently. Is it purely a matter of style? var query = from p in Products where p.Name.Contains("foo") orderby c.Name select p; // or with extension methods: var query = Products .Where(p => p.Name.Contains("foo")) .OrderBy(p => p.Name); They're very similar with the second example being a bit more terse, but perhaps less expressive if you don't know what the => is doing. Other than writing terse code, are

Extension methods syntax vs query syntax

浪子不回头ぞ 提交于 2019-11-26 07:25:17
问题 I\'m trying to get a handle on if there\'s a good time to use standard linq keywords or linq extension methods with lambda expressions. They seems to do the same thing, just are written differently. Is it purely a matter of style? var query = from p in Products where p.Name.Contains(\"foo\") orderby c.Name select p; // or with extension methods: var query = Products .Where(p => p.Name.Contains(\"foo\")) .OrderBy(p => p.Name); They\'re very similar with the second example being a bit more