What does this line mean in C# r => r.Item1? [duplicate] - c#

This question already has answers here:
C# Lambda expressions: Why should I use them?
(17 answers)
Closed 5 years ago.
r => r.Item1 == dFName
Its part of this line
dFName = PropertyUtil.GetName<kUpdate>(r => r.Resolution);
fld2Lup = map.DTField2LookupMap[domType].First(r => r.Item1 == dFName );

That is essentialy..
Return the First item of the "DTField2LookupMap[domType]" collection where the items property "Item1" is equal to dfName
this is a lambda expression: https://msdn.microsoft.com/en-us/library/bb397687.aspx

Related

How to replace foreach with linq [duplicate]

This question already has answers here:
Update all objects in a collection using LINQ
(18 answers)
Closed 16 days ago.
foreach (var item in items)
{
item.category = "All Items";
}
How to replace the above foreach with Linq.
I have tried with following code, but it returns null for the item
_ = items.Select(x => x.item = "All Items")
Note : items is of type IEnumerable<ItemList>
linq is an example of functional programming - in general it does not change the input data it reads it in and outputs new data
you can do this (assuming you have a List to start with)
items = items.Select(item=>new {category="All Items", x=.., y=..}).ToList()
wher x and y are the other fields in ItemList

How can I pass LINQ .Where condition as parameter? [duplicate]

This question already has answers here:
C# Linq where clause as a variable
(5 answers)
Closed 2 years ago.
In LINQ, is it possible to pass .Where conditions as parameter?
IList<Object> obj = persons
.Where(p => p.Text.Contains("x") || p.Text.Contains("y"))
.ToList();
So that more than one dynamic conditions
The single line you have posted is equivalent to the following:
bool filter( Person p )
{
return p.Text.Contains( "x" ) || p.Text.Contains( "y" );
}
IList<Object> obj = persons.Where( filter ).ToList();
I hope this answers your question.

How many times will Linq to Objects iterate over a source? [duplicate]

This question already has answers here:
What are the benefits of a Deferred Execution in LINQ?
(3 answers)
What do they mean when they say LINQ is composable?
(2 answers)
Numbers of iteration generated in a single LINQ query
(1 answer)
Closed 3 years ago.
Say I have a simple Linq query:
var x = words.Where(w => w.Length > 4).Select(w => w.ToUpper()).ToArray();
Will the compiler generate code that iterates over words once, filtering and transforming as it goes, or code that generates an intermediate enumeration and then iterates over that?
What if there's an OrderBy():
var x = words.Where(w => w.Length > 4).OrderBy(w => w).Select(w => w.ToUpper()).ToArray();
I could see the compiler either iterating once over words, filtering and uppercasing words as it goes and merging them into an already sorted IOrderedEnumerable, or I could see it generating an intermediate array, sorting that, and then transforming it.
Rewrite the query as follows to see the flow of the data.
var x = words
.Where(w =>
{
Console.WriteLine("Where: " + w);
return w.Length > 4;
})
.Select(w =>
{
Console.WriteLine("Select: " + w);
return w.ToUpper();
})
.ToArray();

LINQ sort collection with IF statement [duplicate]

This question already has answers here:
Multiple "order by" in LINQ
(7 answers)
Closed 4 years ago.
I want to sort the collection before processing. I used to use this method:
foreach (var item in Items.OrderBy(i => i.property1))
{
...
}
Now i need to sort it by property2, if property values is equals between two items. Is there any method to do that logic via LINQ expression?
Is this what you're after?
foreach (var item in Items.OrderBy(i => i.property1).ThenBy(i => i.property2))
{
...
}

Error in expression [duplicate]

This question already has answers here:
Linq Convert.ToInt32 in Query
(2 answers)
Closed 4 years ago.
Can anyone help me with the following error?
LINQ to Entities does not recognize the method 'Int32
Int32(System.String)' method, and this method cannot be translated
into a store expression.
Below is my code, I am trying in several ways to fix this error, but I have not been successful:
public IEnumerable<Dia1> GetPendenciasByUser(int centroId)
{
var query = Db.Dia1S
.Join(Db.Cadastros, dia1 => dia1.PatientId, cad => cad.PatientId, (dia1, cad) => new { dia1, cad })
.Join(Db.Randomizacao, dia1 => dia1.dia1.PatientId, rand => rand.PatientId, (dia1, rand) => new { dia1, rand })
.Where(s => s.dia1.dia1.dtd1 == null ? (Convert.ToInt32(DateTime.Now - s.rand.RandomizacaoData)) > 1 : (Convert.ToInt32(Convert.ToDateTime(s.dia1.dia1.dtd1) - s.rand.RandomizacaoData)) > 1 )
.Select(s => s.dia1.dia1)
.ToList();
return query;
}
The error message is clear, LINQ doesn't know how to convert the Convert.ToInt32() function to SQL. You can either use direct casting like this:
(int)(DateTime.Now - s.rand.RandomizacaoData)
Or you'll have to execute the query and get the data, then convert it in memory using Convert.ToInt32() as you wish.

Categories