My query is some thing like:
var subQuery = contacts_requests.Where(i => i.requests_usr_id >= 1).Select
(i => i.Usr_contacts_requests_from_usr_id ).ToArray();
var query = biographic_details.Join(profiles_companies, i => i.usr_id, j => j.company_usr_id,
(i,j)=>new{
Usr_bio_usr_id }).where(p=>subQuery.Contains(i.company_usr_id)).ToArray();
I want notcontains operation in place of contains, how can I implement that?
Instead of
p => subQuery.Contains(i.company_usr_id)
use
p => !subQuery.Contains(i.company_usr_id)
Note the ! before the method call. The ! operator (aka Logical negation operator) just negates the result of the following expression. So Contains becomes Not Contains.
To achieve not contains you can use !subQuery.Contains(i.company_usr_id).
Related
Coming from this SO question, I'm trying to have a List (or non-scalar thing, in general) as the value assigned to a Hash key, this way:
my %syns-by-name does Associative[Str,List] = Bq => ("Bq", "becquerel", "becquerels");
my Str #syns = %syns-by-name<Bq>;
That does not work, however. Lists are itemized before being assigned, so the value is always a Scalar. You need to do a workaround to actually make this work:
my %syns-by-name does Associative[Str,List] = Bq => ("Bq", "becquerel", "becquerels");
my #list := <C coulomb coulombs>;
%syns-by-name<C> := #list;
my Str #syns = %syns-by-name<C>;
say #syns;
This returns what we were looking for, a list. However, how could we do that directly on the assignment and convince a list is a list and not an itemized list?
Assuming you don't need mutation afterwards, use a Map instead of a Hash.
my %syns-by-name is Map = Bq => ("Bq", "becquerel", "becquerels");
my Str #syns = %syns-by-name<Bq>;
say #syns; # [Bq becquerel becquerels]
Since there's no expectation that entries in a Map are assignable, it doesn't create Scalar containers for the values.
How about:
role deconting {
method AT-KEY(\key) {
callsame<>
}
}
my %h does deconting = a => <a b c>;
dd $_ for %h<a>; # "a""b""c"
This makes sure that the hash that does the "deconting" role will always return whatever is in the hash decontainerized.
Making it decontainerized on assignment can also be done, but is a lot more tricky as that would need tweaking of at least two methods: STORE and ASSIGN-KEY.
Despite the excellent answers from #Jonathan_Worthington and #Elizabeth_Mattijsen, I wanted to post the code below, which utilizes simple decontainerization:
~$ raku
Welcome to 𝐑𝐚𝐤𝐮𝐝𝐨™ v2020.10.
Implementing the 𝐑𝐚𝐤𝐮™ programming language v6.d.
Built on MoarVM version 2020.10.
To exit type 'exit' or '^D'
> my %syns-by-name = Bq => ("Bq", "becquerel", "becquerels");
{Bq => (Bq becquerel becquerels)}
> my Str #syns = %syns-by-name<Bq>;
Type check failed in assignment to #syns; expected Str but got List (("Bq", "becquerel", ...)
in block <unit> at <unknown file> line 1
> my Str #syns = %syns-by-name<Bq>[];
[Bq becquerel becquerels]
>
I gather there is an academic question here as to how the variable is defined versus how the values of the variable are accessed. However I don't want a casual reader to conclude that Raku is lacking functionality vis-à-vis hashes and lists.
> my %syns-by-name = Bq => ("Bq", "becquerel", "becquerels");
{Bq => (Bq becquerel becquerels)}
> dd $_ for %syns-by-name<Bq>[]
"Bq"
"becquerel"
"becquerels"
Nil
> my $list = <C coulomb coulombs>;
(C coulomb coulombs)
> say $list.WHAT
(List)
> %syns-by-name<C> = $list
(C coulomb coulombs)
> dd $_ for %syns-by-name<C>[]
"C"
"coulomb"
"coulombs"
Nil
>
I hope this answer isn't superfluous and casual readers will benefit. Thank you.
I have a simple List like
List<string> Test=new List<string>() {"A","B","C"}
I am using a simple Linq.Aggregate as under to get a string from the elements of this List as under:-
string Output= Test.Aggregate((First, Next) => First + "\r\n" + Next);
This gives me the result like(new line separated):
A
B
C
However,i want result with a sequence number on each line,ie like this:-
1)A
2)B
3)C
How do i do this using linq?
Select has an overload that will give you the index of the element to work with so you could do Select((x,i)=>String.Format("{0}){1}", i+1,x)). Or in full:
string output= Test.Select((x,i)=>String.Format("{0}){1}", i+1,x)).Aggregate((First, Next) => First + "\r\n" + Next);
One thing worth mentioning though is that string concatenation in a loop (and in the Aggregate counts as in a loop) is generally considered a bad idea for performance reasons. You should consider using alternative methods such as a StringBuilder:
string output = Test
.Aggregate (new StringBuilder(), (sb, x) => sb.AppendFormat("{0}){1}\r\n", lineCount++, x))
.ToString();
I wouldn't use Aggregate here, just a Select to get the index and join the resulting list back together to make a single string, for example:
var output = string.Join("\r\n",
Test.Select((s, index) => $"{index+1}){s}"));
What is the difference between LINQ and Lambda Expressions? Are there any advantages to using lambda instead of linq queries?
Linq is language integrated query. When using linq, a small anonymous function is often used as a parameter. That small anonymous function is a lambda expression.
var q = someList.Where(a => a > 7);
In the above query a => a > 7 is a lambda expression. It's the equivalent of writing a small utility method and passing that to Where:
bool smallMethod(int value)
{
return value > 7;
}
// Inside another function:
var q = someList.Where(smallMethod);
This means that your question is really not possible to answer. Linq and lambdas are not interchangeable, rather lambdas are one of the technologies used to implement linq.
LINQ is Language integrated query, where is lamda expression are similar to Annonymous method for .Net 2.0.
You can't really compare them may be you are confused because LINQ is associated with lamda expression most of the time.
You need to see this article: Basics of LINQ & Lamda Expressions
EDIT: (I am not so sure, but may be you are looking for the difference between Query Syntax and Method Sytnax)
int[] numbers = { 5, 10, 8, 3, 6, 12};
//Query syntax:
IEnumerable<int> numQuery1 =
from num in numbers
where num % 2 == 0
orderby num
select num;
//Method syntax:
IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);
In the above example taken from MSDN, Method Sytnax contains a lamda expression (num => num % 2 == 0) which works like a method, takes number as input and returns true if they are even.
They both are similar, and in the words of Jon Skeet, they both compile to similar code.
In a nutshell:
LINQ is a quering technology (Language Integrated Query). LINQ makes extensive use of lambda's as arguments to standard query operator methods such as the Where clause.
A lambda expression is an anonymous function that contain expressions and statements. It is completely separate and distinct from LINQ.
I am having a bit of trouble figuring out the exact syntax to use string.compare in the Where clause of a linq query. Below is what I have so far.
filteredApplications = AllApplications.Where(x => x.Name.Contains(string.Compare(x.Name, txtSearch.Text, StringComparison.OrdinalIgnoreCase))).ToList();
Is this even possible or am I barking up the wrong tree?
Rhonda
If you want to check to see if Name contains the search text:
AllApplications.Where(x => x.Name.ToUpperInvariant().Contains(txtSearch.Text.ToUpperInvariant()))).ToList();
If you want to check for equality:
AllApplications.Where(x => string.Equals(x.Name, txtSearch.Text, StringComparison.OrdinalIgnoreCase)).ToList();
In your original query, you were checking to see if x.Name contains the result of string.Compare. I assume you weren't trying to do this, since string.Compare returns an integer. string.Compare is used primarily for determining sort order.
I believe you're looking for Equals if you're looking to match by equality:
filteredApplications = AllApplications.Where(x => x.Name.Equals(txtSearch.Text, StringComparison.OrdinalIgnoreCase)).ToList();
See the two Linq (to SharePoint) code samples below.
The only differences are the highlighted sections of code. The first statement works as expected with a hard-coded where clause, but the 2nd set of code throws the error “Value does not fall in within the expected range” when I try to do a count on the items. What am I missing?
Works
relatedListItems = dc.GetList<GeneralPage>("Pages")
.Where(x => x.RelatedPracticesTitle.Any(y=>y=="Foo"))
if (relatedListItems.Count() == 0)
{…}
Fails - “Value does not fall within the expected range”
Func<GeneralPage, bool> f = x => x.RelatedPracticesTitle.Any(y => y == "Foo");
relatedListItems = dc.GetList<GeneralPage>("Pages")
.Where(f)
if (relatedListItems.Count() == 0)
{…}
If it's LINQ to Sharepoint, presumably that means it should be using expression trees, not delegates. Try:
Expression<Func<GeneralPage, bool>> f =
x => x.RelatedPracticesTitle.Any(y => y == "Foo");
relatedListItems = dc.GetList<GeneralPage>("Pages").Where(f);
By the way, it's generally a better idea to use Any() rather than Count() if you just want to find out if there are any results - that way it can return as soon as it's found the first one. (It also expresses what you're interested in more clearly, IMO.)
In the first case, you're using the Expression<Func<GeneralPage, bool>> overload and pass an expression which I assume LINQ to SharePoint will try to convert to CAML and execute.
In the second case, you're passing the plain Func<GeneralPage, bool> so LINQ to SharePoint can't figure out how to compose a query (it only sees the delegate, not the expression).