I have following code and want to convert it to Query expression. (Exp. taken from msdn linq 101 sample)
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var shortDigits = digits.Where((digit, index) => digit.Length < index);
foreach (var d in shortDigits)
{
Console.WriteLine("The word {0} is shorter than its value.", d);
}
Please help me out.
You cannot do this, because query syntax does not support Where operator with index parameter. See remarks section at MSDN:
In query expression syntax, a where (Visual C#) or Where (Visual
Basic) clause translates to an invocation of
Where<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>).
If you need index in query syntax, you can define it outside of query:
int index = 0;
var shortDigits = from d in digits
where d.Length < index++
select d;
Related
I need to show list of authors group by last name first letter.
e.g.
A
Kim, Ami
Dim, Amaiar
jin, Amairaz
B
Bin, Bom
Kin, Bomo
C
Cin, Ci
Con, Co
....
Could some one please help me what's the best way to solve the above problem?
If you want to group by, use GroupBy, I assumed you want the output to be ordered (OrderBy), Change the GroupBy expression to match your exact requirment:
List<String> names = new List<String>{"Bill", "Mark", "Steve", "Amnon", "Benny"};
foreach(var g in names.GroupBy(name => name.First()).OrderBy(g => g.Key)){
Console.WriteLine(g.Key);
g.OrderBy(name => name).ToList().ForEach(Console.WriteLine);
}
Will output:
A
Amnon
B
Bill
Benny
M
Mark
S
Steve
You can use GroupBy extension method over Linq object to get the desire result.
List<string> firstNames = new List<string>(){ "Ami", "Amaiar","Amiraz","Bom","Bomo","Ci","Co" };
var groups = firstNames.GroupBy(x=>x[0]);
foreach (var element in groups)
{
Console.WriteLine("{0}", element.Key);
foreach (var word in element)
Console.WriteLine(" {0}", word);
}
I need to generate a string that has a comma delimited list, but no comma after the last element.
var x = new List<string>() { "a", "b", "c" };
should yield:
a,b,c
Yes, a very simple thing to do using "normal" techniques, but I hope with linq there is a more elegant way.
var cols =context.Database.SqlQuery<String>("select Column_Name from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = {0};", prefix + table);
No need Linq, just use String.Join
String.Join(",", new List<string>() { "a", "b", "c" });
String class provide Join method to join string array with delimiter.
Code:
var x = new List<string>() { "a", "b", "c" };
String.Join(",",x.ToArray());
documentation: http://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx
Although I strongly recommend you use the answer of Siva Charan, just for information here's an implementation in LinQ using Enumerable.Aggregate Method (IEnumerable, Func):
var result = x.Aggregate((c, n) => c + "," + n);
How do you rewrite this in lambda?
int[] productList = new int[] { 1, 2, 3, 4 };
var myProducts = from p in db.Products
where productList.Contains(p.ProductID)
select p;
Assuming that by "with lambda" you mean the "query syntax", you can rewrite your query like this:
var myProducts = db.Products.Where(p => productList.Contains(p.ProductID));
Same thing just move the logic within a Where call.
var myProducts = db.Products.Where(p => productList.Contains(p.ProductID));
http://msdn.microsoft.com/en-us/library/vstudio/bb397947.aspx
^ a quick read that compares query syntax to method syntax for a query to two.
I need some help from Everyone
I have a string: "123456"
how can i get seperately "1", "2", "3", "4", "5", "6" by using Linq?
Tks a lot.
var str = "123456";
var digits = str.Select(c => c.ToString()).ToArray();
"123456".First()
Also, you have the option to include a predicate function. For example,
"123456".First(n => n > '1')
But you may consider using the foreach statement instead. I'm not sure if that is considered part of LINQ though.
You could use this if you want it as a LINQ query:
IEnumerable<string> query =
from c in "123456"
select c.ToString();
If you're happy to have an enumerable of characters you could just do this:
IEnumerable<char> query =
from c in "123456"
select c;
how do i join the results in a IEnumerable to a single string?
the IEnumerable contains 20 single letters, and i want it to combine it to a single string.
And out of curiousity: how would i join it with a separator, for example if the IEnumerable contains the strings a b c d e how can i join it to a,b,c,d,e?
Michel
Try this:
IEnumerable<string> letters = new[] { "a", "b", "c", "d", "e" };
string separator = ", ";
string withSeparator = String.Join(separator, letters.ToArray());
string withoutSeparator = String.Join(String.Empty, letters.ToArray());
Also, with 4.0 .NET there's a new simpler overload available: String.Join Method (String, IEnumerable<String>) so you can skip the ToArray() call.