How can i get element in string by Linq - linq

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;

Related

Rails5 ActiveRecord error: TypeError: can't quote Array

I'm trying to check to see if there's a pre-existing Update.
The field is receiving_account_id, and it's looks like
["1", "3", "4"]
and my query is
if Update.where("band_id = ? and fan_id = ? and receiving_account_id = ? and response = ?", "#{#remove_member.band_id}", "#{#remove_member.fan_id}", "#{[#remove_member.receiving_account_id]}", "none").any?
i've tried all variations of "#{#remove_member.receiving_account_id}, and i get some version of a malformed array literal.
edit: if i go
[#remove_member.receiving_account_id]
in the query, i get the type error. and if i do
receiving_account_id && ARRAY[?]
i get
receiving_account_id && ARRAY['["1", "3", "4"]']
which i believe to mean includes in the array, as if the array includes that array, so doesn't find it.
Survey says...
Update.where("receiving_account_id && ARRAY[?]", #remove_member.receiving_account_id).any?

c# group by alphabets

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);
}

Use Hash with Redundant Items in sqlite3 Bind

I'm trying to run an INSERT query such as the following:
Query = "INSERT INTO t (x,y,z) VALUES (:aval,:bval,:cval)"
Using the following format, I can use a hash to insert the actual values:
db.execute(Query,{"aval" => "1", "bval" => "2", "cval" => "3"})
My problem is that the values are already in a hash that has some redundant values, e.g.:
{"aval" => "1", "bval" => "2", "cval" => "3", "dval" => "4"}
Since dval is not one of the required parameters, I get the error -
SQLite3::Exception: no such bind parameter
Of course, I may be wrong and the error may be due to a different reason.
It would be great if there were a way to overcome this using SQLite3. Alternatively, a method for creating a "trimmed" copy of the has with only the required parameters would also be OK.
You should write as -
Query = "INSERT INTO t (x,y,z) VALUES (?,?,?)"
hash = {"aval" => "1", "bval" => "2", "cval" => "3"}
db.execute(Query, hash.values_at("aval", "bval", "cval" ))
Read this execute( sql, *bind_vars ) {|row| ...} documentation.

Using linq to build a comma delimited string

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 to insert an array of id's into linq-to-sql query line in .net mvc 3

i want to get an array of id's which is like ["15", "26", "37", "48", "90"] and i want to get my remaining items from my remaining table that doesnt includes these supplier id's..
here what i done so far:
string[] arrgroupdetails;
arrgroupdetails = dataContext.GroupDetails.Select(c => c.supplier_id).ToArray();
var items = from thingies in dataContext.remainings where thingies.supplier_id.ToString() != arrgroupdetails.Any().ToString() select thingies;
so how can i achive this?
By heart, so do check syntax but someething like this should work:
var items = from thingies in dataContext.remainings
where !arrgroupdetails.Contains(thingies.supplier_id.ToString())
select thingies;

Resources