Reputation: 5157
I'm not sure if this would be desirable, but is there a LINQ "way" of replacing the following:
foreach (var u in users)
context.Users.Add(u);
with something like
context.Users.Add(....);
Upvotes: 1
Views: 292
Reputation: 34103
Assuming users
is type List<User>
you just do
users.ForEach(u => context.Users.Add(u));
though you don't need to use LINQ. You could just do
context.Users.AddRange(users);
Upvotes: 3
Reputation: 118937
If Users
is a DbSet
then you can probably do without looping at all by using AddRange
:
context.Users.AddRange(users);
However if you really want to use Linq, then you can also do this:
users.ToList().ForEach(u => context.Users.Add(u));
Or the shorthand version:
users.ToList().ForEach(context.Users.Add);
Upvotes: 0
Reputation: 125620
You don't need LINQ. You need to use AddRange
instead of Add
:
context.Users.AddRange(users);
Assumption: you're using Entity Framework and Users
is of type DbSet<T>
Upvotes: 2