linq - C# Filter List to remove any double object -
have searched ant tested many examples in forum can't working method.
i using linq bulk insert list of entity classes (remotereadings).
due unique constraints need filter out items inserted.
uniqiuness composed of 2 columns meterid , datetime in remotereadings table.
// approx 5000 records (i need in batches of 2000 due // constraint in l2s,but can after working) list<remotereading> lst = createlistfromcsv(); // option 1: // not work comparing memory list db list. need use contains() method. // trying accomplish infollowing examples. list<remotereading> mylst = (from ri in db.remotereadings l in lst l.meterid = ri.meterid && l.date = r.date select ri).tolist(); //// // option2: // list db in memory lst list<remotereading> mylst = (from ri in db.remotereadings // in list comparing meaterid , datemeaured (from l in lst select /// here ! /// select ri).tolist<remoteinterconnectorreading>(); // option3: // list lst not in database // bit confused here ! // tried remove list duplicates: list<remotereading> result = list<remotereading>)mylst.except(lst).tolist<remotereading>(); // db.remotereading.insertallonsubmit(result); db.submitchanges();
any please?
due limitations in ef, can't join db query in-memory list. also, contains
can used primitive list. need make efforts find duplicates on 2 columns.
var newitems = createlistfromcsv(); var meterids = newitems.select(n=> n.meterid).distinct().tolist(); var dates = newitems.select(n=> n.date).distinct().tolist(); var probablematches = (from ri in db.remotereadings (meterids.contains(ri.meterids) || dates.contains(ri.date) select new {ri.merterid, ri.date}).tolist(); var duplicates = (from existingri in probalematches join newri in newitems on new {existingri.meterid, existingri.date} equals {newri.meterid, newri.date} select newri).tolist(); var insertlist = newitems.except(duplicates).tolist(); db.remotereadings.insert(insertlist); // or whatever
Comments
Post a Comment