Use linq to generate direct update without select

前端 未结 7 864
感情败类
感情败类 2020-11-29 05:34

G\'day everyone.

I\'m still learning LINQ so forgive me if this is naive. When you\'re dealing with SQL directly, you can generate update commands with conditionals

相关标签:
7条回答
  • 2020-11-29 06:10

    You can use Entity Framework Extensions library, it supports batch update and batch merge, however the library is not free:

    PM > Install-Package Z.EntityFramework.Extensions

    using Z.EntityFramework.Plus;
    
    ...
    
    dc.Products
        .Where(q => q.Type == 1)
        .Update(q => new Product { Count = 0 });
    
    0 讨论(0)
  • Linq 2 SQL doesn't have direct insert/update/delete equivalents of SQL. In V1 the only updates you can do using linq is thought SubmmitChanges on the context or if you fallback to sql.

    However some people have tried to overcome this limitation of linq using custom implementations.

    Linq batch update.

    0 讨论(0)
  • 2020-11-29 06:19

    Try this :

    dbEntities.tblSearchItems
         .Where(t => t.SearchItemId == SearchItemId)
         .ToList()
         .ForEach(t => t.isNew = false);
    dbEntities.SaveChanges();
    
    0 讨论(0)
  • 2020-11-29 06:23

    The PLINQO (http://plinqo.com) framework is using the LINQ batch update to perform updates

    context.Task.Update(t => t.Id == 1, t2 => new Task {StatusId = 2});

    This will perform a Update Task Set StatusId = 2 Where Id = 1

    0 讨论(0)
  • 2020-11-29 06:30

    Use this extension method: EntityExtensionMethods.cs

    public static void UpdateOnSubmit<TEntity>(this Table<TEntity> table, TEntity entity, TEntity original = null)
        where TEntity : class, new()
    {
        if (original == null)
        {
            // Create original object with only primary keys set
            original = new TEntity();
            var entityType = typeof(TEntity);
            var dataMembers = table.Context.Mapping.GetMetaType(entityType).DataMembers;
            foreach (var member in dataMembers.Where(m => m.IsPrimaryKey))
            {
                var propValue = entityType.GetProperty(member.Name).GetValue(entity, null);
                entityType.InvokeMember(member.Name, BindingFlags.SetProperty, Type.DefaultBinder,
                    original, new[] { propValue });
            }
        }
    
        // This will update all columns that are not set in 'original' object. For
        // this to work, entity has to have UpdateCheck=Never for all properties except
        // for primary keys. This will update the record without querying it first.
        table.Attach(entity, original);
    }
    

    To use it, make sure the entity object that you pass to UpdateOnSubmit method has all the primary key properties set for the record you want to update. This method will then update the record with the remaining properties from the entity object without pulling the record first.

    After calling UpdateOnSubmit, make sure to call SubmitChanges() for changes to apply.

    0 讨论(0)
  • 2020-11-29 06:32

    No, neither LINQ nor LINQ to SQL has set-based update capabilities.

    In LINQ to SQL, you must query for the object you wish to update, update the fields/properties as necessary, then call SubmitChanges(). For example:

    var qry = from product in dc.Products where Product.Name=='Foobar' select product;
    var item = qry.Single();
    item.Count = 0;
    dc.SubmitChanges();
    

    If you wish to do batching:

    var qry = from product in dc.Products where Product.Type==1 select product;
    foreach(var item in qry)
    {
      item.Count = 0;
    }
    dc.SubmitChanges();
    

    Alternatively, you could write the query yourself:

    dc.ExecuteCommand("update Product set Count=0 where Type=1", null);
    
    0 讨论(0)
提交回复
热议问题