add results to linq query - c#

I'm querying a datatable using linq, I then query the results to filter out what I want, the problem is that I need to query it for each value of an array these values are like this ,2, or ,22, or ,21, etc
so I usually do this
results = from a in results
where a.countryId.ToString().Contains(value)
select a;
what I would like to do is this
foreach(string str in arrayval)
{
results += from a in results
where a.countryId.ToString().Contains(str)
}
can anyone help or give me some clues
thanks

Looks like you want to select results based on comparison from the array values. Something like. Select * from table where ID in (1,2,3). Instead of concatenating results you can try the following query.
var result = from a in result
where arrayval.Contains(a.CountryId.ToString())
select a;
if your arrayval is an int type array then you may remove .ToString() at the end of a.CountryID

Try this
results.Where(a=> arrayval.Contains(a.CountryId.ToString()).Aggregate("", (a,b)=> a+b);

I would create a list of your array values e.g.
List<string> list = new List<string>()
{
"2",
"12",
"20",
//etc...
};
and then do
var result = results.Where(p => list.Contains(p.countryId.ToString()));
Basically you are only select countryId's that are contained in the list.

Related

Convert linq query to Generic string array

I know that I can cast my linq query to an array or list but this doesn't seem to help.
Here is my query:
var bracct = from DataRow x in _checkMasterFileNew.Rows
select new {BranchAccount = string.Format("{0}{1}", x["Branch"], x["AccountNumber"])};
When I attempt to convert it to a list or array:
List<string> tstx = bracct.ToList();
or this:
string[] stx = bracct.ToArray();
If give me this:
I am assuming I need to change my query but I'm not sure the best way to hanlde it. How do I get it to a generic collection of strings?
It won't work because you've created an anonymous type with 1 property which is a string. Instead, If all you want is to convert it into a List<string> do:
var bracct = (from DataRow x in _checkMasterFileNew.Rows
select string.Format("{0}{1}", x["Branch"], x["AccountNumber"])).ToList();
And if using c# 6.0 you can use string interpolation:
var bracct = (from DataRow x in _checkMasterFileNew.Rows
select $"{x["Branch"]}{x["AccountNumber"]}").ToList();
Your query is creating an anonymous type with a single member BranchAccount. If you actually just want a string, then just select that instead:
var bracct =
from DataRow x in _checkMasterFileNew.Rows
select string.Format("{0}{1}", x["Branch"], x["AccountNumber"]);
And now your ToList() call will return List<string>:
List<string> tstx = bracct.ToList();
You must select the property you are assigning the string to before performing ToList(), or remove the anonymous type and select string.Format() directly
Try this:
List<string> tstx = bracct.Select( x => x.BranchAccount ).ToList();

linq - selecting elements not equal to something

Suppose I have a collection of strings.
How do I select all the elements that don't contain a certain parameter value?
List<string> TheList = .....
var TheCleanList = (from s in TheList
where s != parameter
select s).ToList();
I was thinking about where s!= parameter but I'm wondering if there's a cleaner way to do it.
Thanks.
If you don't need a new list you don't need Linq for this - use Remove()- this avoids having to create a new list:
If you want to remove all strings that are equal to Parameter:
TheList.RemoveAll(s => s == Parameter);
If you want to remove all strings that contain Parameter (not clear from your question):
TheList.RemoveAll(s => s.Contains(Parameter));
You mean:
List<string> TheList = .....
var TheCleanList = (from s in TheList
where !s.Contains(parameter)
select s).ToList();
You can use String.Contains
var TheCleanList = (from s in TheList
where !s.Contains(parameter)
select s).ToList();
Or
var TheCleanList = TheList.Where(s => !s.Contains(parameter)).ToList();
String.Contains is case-sensitive. If you want a case-insensitve:
string lower = parameter.ToLower();
...
where s.ToLower().Contains(lower)

How to Union List<List<String>> in C#

I'm having a List<List<String>>, and which contains
{ {"A" , "B" },
{"C" , "D" }
}
I need to union all the innerlist into another list
So the resulting List<String> will contain
{"A","B","C","D"}
Now im using for loop to do this
Is there any way to do this Using LINQ or Lambda Expression.
Please help me to do this.
Thanks in advance.
Not Exactly a Union, but you can try this
YourList.SelectMany(l=>l).Distinct()
List<List<string>> collections = new List<List<string>>()
{
new List<string>(){"A" , "B" },
new List<string>() {"C" , "D" }
};
var list = collections.SelectMany(x => x).ToList();
SelectMany builds up a expression tree that when evaluated flattens the list of list to a single list of combined members.
ToList forces the expression tree to be evaluated and which results in a List.
If you want to eliminate duplicates you can add a Distinct call before the call to 'ToList()'
You can use the SelectMany extension method.
List<List<String>> masterList = { {"A" , "B" }, {"C" , "D" } };
IEnumerable<string> results = masterList.SelectMany(l => l);
var result = myLists.SelectMany(l => l);
How about Aggregate?
myLists.Aggregate((left, right) => left.Union(right));
To me, this is more expressive than using SelectMany, because it is telling you exactly what you are doing: Aggregate your list of lists by calling union on them all.
Just for kicks:
(from list in theList from e in list select e).Distinct().ToList()
This is of course the same solution as #Alexander Taran's, just with query syntax instead of lambda syntax. (Or at least it should be – I don't have my LINQPad handy.)

Orderby() not ordering numbers correctly c#

I am writing an app for my company and am currently working on the search functionality. When a user searches for an item, I want to display the highest version (which is stored in a database).
The problem is, the version is stored as a string instead of int, and when I do an OrderBy(q=>q.Version) on the results, they are returned like
1
10
11
2
3
...
Obviously 2 comes before 10.
Is there a way for me to cast the version as an integer or is there a simple IComparer out there? I couldn't find anything substantial thus far.
I tried doing this:
var items = (from r in results
select r).OrderBy(q => Int32.Parse(q.Version));
This compiles but doesn't work.
Int32.Parse is not supported by the LinqToSql translator. Convert.ToInt32 is supported.
http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx
http://msdn.microsoft.com/en-us/library/bb882655.aspx
Your problem is somewhere else, the following works:
new[] { "1", "10", "2", "3", "11" }
.OrderBy(i => int.Parse(i))
.ToList()
.ForEach(Console.WriteLine);
If your problem is LINQ to SQL, then what is happening is CLR is trying to create SQL out of your LINQ and doesn't understand int.Parse. What you can do is first get the data from SQL then order it once all data is loaded:
var items = (from r in results
select r)
.ToList()
.OrderBy(q => Int32.Parse(q.Version));
Should do it.
If you're unable to change your table definition (so the version is a numeric type), and your query really is as listed (your not using skip, or take, or otherwise reducing the number of results), the best you can do is call "ToList" on the unsorted results, which when you then apply an OrderBY lambda to it will take place in your code, rather than trying to do it at the SQL Server end (and which should now work).
There's an awesome piece of code that does a great job when it comes to natural sorting. Its name is AlphanumComparator.
Sample code:
var ordered = Database.Cars.ToList().OrderBy(c => c.ModelString, new AlphanumComparator());
Note that the list must be in memory.
If you get the C# version, do this:
AlphanumComparator : IComparer<string>
and
public int Compare(string x, string y)
Why are you sorting in a lambda? Why don't you just sort in the query?
var query = from r in items
orderby int.Parse( r )
select r;
Now that we know you are using LINQ to SQL, you might consider making a standard SQL call on this one by doing something like:
Select ..., Cast( TextWhichShouldBeIntCol As int ) As IntCol
From ...
Or even
Select ..., Cast( TextWhichShouldBeIntCol As int ) As IntCol
From ...
Order By Cast( TextWhichShouldBeIntCol As int )
That will bleed into your LINQ as an int (and if you use the second iteration, be ordered). That avoids having to go through the resultset twice in LINQ (once for querying, once for ordering).
I made a test. I have the following code.
string[] versions = { "1", "2", "10", "12", "22", "30" };
foreach (var ver in versions.OrderBy(v => v))
{
Console.WriteLine(ver);
}
As expected the result is 1, 10, 12, 2, 22, 30
Then lets change versions.OrderBy(v => v)) to versions.OrderBy(v => int.Parse(v))). And it works fine: 1, 2, 10, 12, 22, 30
I think your problem is that you have nondigit chars in your string like '.'. What kind of exception do you get?
try this:
var items = results.(Select(v => v).OrderBy(v => v.PadLeft(4));
that'll work in Linq2Sql
Why are you sorting if you only need "the highest version"? It sounds like you could avoid some overhead if you used Max().
Also, you really should change the column type to integer.
var items = (from r in results
select r).OrderBy(q => Convert.ToInt32(q.Version));
Definitely run......
It sounds like you have a text value instead of a numeric value.
If you need to sort, you can try:
var items = (from r in results
select r);
return items.OrderBy( v=> Int.Parse(v.Version) );
var query = from r in items
let n = int.Parse(r)
orderby n
select n;
var items = (from v in results
select v).ToList().OrderBy(x => int.Parse(x.Version));

Return Multiple Columns in Linq to Sql?

How do I return multiple columns with linq to sql in C#?
I tried to end my query with
select new { A.Product, A.Qty };
but this returns some anonymous type and I am not sure what the heck what to do with this, How to return it and how to extract information out of it. I want to put it in some sort of array.
thanks
Are you trying to return the data from a method?
If so, you should just end the query with select A, which will produce the same type that A is.
If not, you can use the anonymous type the same way you use a regular type.
For example:
var results = from ... select new { A.Product, A.Qty };
foreach(var thing in results) {
Console.WriteLine("{0}: {1}", thing.Product, Thing.Qty);
}
EDIT: To make it into a list, call ToList, like this:
var resultsList = results.ToList();
Call tolist() on the query

Categories