Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
List<GlobalFieldDatabase> fin = new List<GlobalFieldDatabase>();
          foreach(var t in bg){
                 if(UserEntites.ContainsKey(t.BudgetEntityCode)){
                     if (UserGlobalFields.ContainsKey(t.GlobalFieldCode))
                     {
                         GlobalFieldDatabase g = new GlobalFieldDatabase();
                         g.BudgetEntityId = UserEntites[t.BudgetEntityCode];
                         g.GlobalFieldId = UserGlobalFields[t.GlobalFieldCode];
                         g.GlobalFieldValue = t.GlobalFieldValue;
                         fin.Add(g);
                     }
                 }
                 }


What I have tried:

I have tried but couldn't accomplish the task wasted lots of time TIA
Posted
Updated 5-Mar-16 9:54am

1 solution

It's possible of course but it doesn't gain you much in terms of clarity or lines of code savings (and it also isn't faster, as LINQ isn't inherently faster than a conventional approach):
C#
List<GlobalFieldDatabase> fin =
    (from t in bg
     where UserEntites.ContainsKey(t.BudgetEntityCode)
     && UserGlobalFields.ContainsKey(t.GlobalFieldCode)
     select new GlobalFieldDatabase()
     {
         BudgetEntityId = UserEntites[t.BudgetEntityCode],
         GlobalFieldId = UserGlobalFields[t.GlobalFieldCode],
         GlobalFieldValue = t.GlobalFieldValue
     })
    .ToList();


Edit: By the above reservations I don't mean that I would advise against using LINQ here, just that there is no objective benefit and you'd have to decide yourself which approach to use based on personal preference.
 
Share this answer
 
v2
Comments
MYQueries1 5-Mar-16 16:16pm    
List<globalfielddatabase> fin =
(from t in bg.AsParallel()
where UserEntites.ContainsKey(t.BudgetEntityCode)
&& UserGlobalFields.ContainsKey(t.GlobalFieldCode)
select new GlobalFieldDatabase()
{
BudgetEntityId = UserEntites[t.BudgetEntityCode],
GlobalFieldId = UserGlobalFields[t.GlobalFieldCode],
GlobalFieldValue = t.GlobalFieldValue
})
.ToList();

Linq isn't faster then foreach approach?
I think i can use AsParallel() to acheive paralleism to the code u given
Sascha Lefèvre 5-Mar-16 16:21pm    
> Linq isn't faster then foreach approach?
No, there's still "the same" foreach-loop happening, just "inside" of Linq.

> I think i can use AsParallel() to acheive paralleism to the code u given
That could potentially be faster, but not neccessarily, as the CLR dynamically decides whether parallelism will actually be used. You'd have to try it.
MYQueries1 5-Mar-16 16:33pm    
I have datatable which contains columns A and Column B. How i can write linq query to match the datatable["A"]=BudgetEntityId and datatable["B"]=GlobalFieldId.
Bascially i want the rows from the datatable to match aganist the list of object fin.
How i can do that? foreach is better or LInq is better
Sascha Lefèvre 5-Mar-16 16:53pm    
from row in dataTable.AsEnumerable()
where row.Field<int>("A") == BudgetEntityId
&& row.Field<int>("B") == GlobalFieldId
...

(change <int> to the required type if other than int)

I mostly choose between foreach (or other types of loops) and Linq in terms of code clarity - which obviously is a subjective matter.
MYQueries1 5-Mar-16 23:01pm    
it should also iterate List fin all the matching rows from the datatable aganist the list fin . I need that result

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900