I have a query:
var q = (from c in session.db.students
where c.id==5
select c);
How can I delete this record? I've tried deleteonsubmit, deleteobject, delete... but none of them are not known.
For example in code below, delete not known:
foreach (student s in q)
{ s.deleteobject;}
session.db.savechanges();
or:
var q = (from c in session.db.students
where c.id==5
select c).deleteOnSubmit();
None of them is not defined.... where is the problem?
You should use Remove:
var q = (from c in session.db.students
where c.id==5
select c);
foreach(student s in q)
{
session.db.students.Remove(s);
}
session.db.SaveChanges();
You want to call .remove([record]) on the DbContext object. Using your code you would do the following:
var q = (from c in session.db.students where c.id == 5 select c);
foreach (student s in q)
{
session.db.Remove(s);
}
session.db.SaveChanges();
or using method based querying (to remove a single record with id 5):
var s = session.db.students.Where(p => p.id == 5).FirstOrDefault();
if(s != null) { session.db.Remove(s); }
session.db.SaveChanges();
Look this answer. maybe it will help
https://stackoverflow.com/a/17723658/4571664
then also you can use this
((DbContext)dbContext).Set<objectName>().Remove(singleObject);
dbContext.SaveChanges();
if you need multiple object delete, you can use this code in foreach.
Try using dbcontext, you can use remove or removerange.
using (var MyContext= new CRMDBContext(connString))
{
MyContext.[Table].RemoveRange(MyContext.[Table].Where(x => x.[column]== [column]));
MyContext.SaveChanges();
}
You can use Remove() or RemoveRange() methods on your context for deleting objects.
If you have collection of objects:
session.db.students.RemoveRange(collectionOfStudents);
session.db.SaveChanges();
If you have just one object:
session.db.students.Remove(oneStudent);
session.db.SaveChanges();
Related
How can I make the performance better of the code below?
I'm loading the carItems from an external webservice in a list.
carItem is checked whether it exists in EF.
If carItem is new, then it's mapped to carsCol and added to the database. What are some easy ways to improve the performance of this using code?
carItems = carItems.Where(x => x.Name == "Tesla");
// Filter existing cars
List<Car> carsCol = new List<Car>();
foreach (var item in carItems)
{
if (GetById(item.Id) == null)
{
carsCol.Add(item);
}
}
Entities.AddRange(carsCol);
Depending on the situation, you can try to find out which ids already exist in the database by making a single query before the foreach.
var newCarItemIds = carItems.Select(x => x.Id);
var alreadyExistentCarItemIds = Entities.CarItems.Where(x => newCarItemIds.Contains(x.Id)).Select(x=>x.Id);
foreach(var item in carItems)
{
if(!alreadyExistentCarItemIds.Contains(x))
{
carsCol.Add(item);
}
}
You can use Distinct Linq functions: example
You must to implement IEquatable interface
I would propose this. You can do a left outter join with the cars that you get from your API and the existing one. Than you get the new cars that will be added.
var newCars = (from c in carItems
join e in Entities.CarItems on c.Id equals e.Id into g
from x in g.DefaultIfEmpty()
select new { c, IsNew = x == null }).Where(x = x.IsNew).ToList()
With that you do only one access to the database. Also when working with IEnumerable is always good to convert it to either a list or array, that way each time that you iterates through that object you don't run a query in your database.
I just have a very simple situation where all I need is to delete record using Linq2Entities. I tried to do some research and still can't figure out the right way to do it.
Here's my simple code:
[DataObjectMethod(DataObjectMethodType.Delete)]
public void DeleteEmployee(Employee z)
{
using (var ctx = new MyEntity())
{
var x = (from y in ctx.Employees
where y.EmployeeId == z.EmployeeId
select y).FirstOrDefault();
ctx.DeleteObject(x);
ctx.SaveChanges();
}
}
[DataObjectMethod(DataObjectMethodType.Select)]
public List<Employee> GetAllEmployee()
{
using (var ctx = new MyEntity())
{
var x = from y in ctx.Employees
select y;
return x.ToList();
}
}
I can delete a particular record if for example I assign y.EmployeeName == "Harold Javier" to the Delete method above, but when I assign y.EmployeeId == z.EmployeeId to the above code, the delete doesn't work. (Note: EmployeeId is the primary key of the Employee table)
I think this is better option of delete
using (var ctx = new MyEntity())
{
var x = (from y in ctx.Employees
orderby y.EmployeeId descending
select y).FirstOrDefault();
ctx.Employees.Remove(x);
ctx.SaveChanges();
}
at my side DeleteObject is not working so i use Remove
you first need to verify that if a record exists before you actually delete it;
[DataObjectMethod(DataObjectMethodType.Delete)]
public void DeleteEmployee(Employee z)
{
using (var ctx = new MyEntity())
{
var x = (from y in ctx.Employees
where y.EmployeeId == z.EmployeeId
select y).FirstOrDefault();
if(x!=null)
{
ctx.Employees.DeleteObject(x);
ctx.SaveChanges();
}
}
}
Always check for nulls, before you delete something. Because a user may change the Id (at querystring) and try different combinations.
The above answer may be outdated... The DeleteObject method does not seem to exist in the current version of ENtity Framework. I had to use the Remove method.
var delobj = db.mytable.Where(p => p.ServiceLocation == serviceLocationID).SingleOrDefault();
db.myTable.Remove(delobj);
#Harold, I know this post is quite old, but I feel it is important to address your original question and answer. Your solution may have worked in your situation, but there are a couple of problems.
First, your original code was selecting the record to delete based on a passed in parameter. Your solution is deleting the record with the largest EmployeeId. That might be what you wish, but not likely.
The second issue is that two database accesses are required to accomplish the delete. The first is get the entity to delete the second to actually perform the delete.
The following code snippet will eliminate the need to to the read and will delete employee "z". This should yield the desired result and perform much better.
public void DeleteEmployeeId(Employee z)
{
using (var ctx = new MyEntityContext())
{
var x = new Employee{ EmployeeId = z.EmployeeId };
ctx.Entry(x).State = EntityState.Deleted;
ctx.SaveChanges();
}
}
I decided to answer my own question.
My delete function worked when I did the following:
using (var ctx = new MyEntity())
{
var x = (from y in ctx.Employees
orderby y.EmployeeId descending
select y).FirstOrDefault();
ctx.Employees.DeleteObject(x);
ctx.SaveChanges();
}
I know there could be a better approach than this, but it works for me for the mean time.
The following worked for me:
MyList.RemoveAll(x => x.MyField == 0);
This is probably because the context is different on each request (var ctx = new MyEntity()). Try using this
public static class ObjectContextPerHttpRequest
{
public static TestCasesModelContainer Context
{
get
{
string objectContextKey = HttpContext.Current.GetHashCode().ToString("ObjectContextPerHttpRequest");
if (!HttpContext.Current.Items.Contains(objectContextKey))
{
HttpContext.Current.Items.Add(objectContextKey, new TestCasesModelContainer());
}
return HttpContext.Current.Items[objectContextKey] as TestCasesModelContainer;
}
}
}
And delete is like
public static void Delete(Testcase tc)
{
var db = ObjectContextPerHttpRequest.Context;
db.DeleteObject((from p in db.TestcaseSet
where p.Id == tc.Id
select p).Single());
db.SaveChanges();
}
Basically I'm trying to do this in LINQ to SQL;
SELECT DISTINCT a,b,c FROM table WHERE z=35
I have tried this, (c# code)
(from record in db.table
select new table {
a = record.a,
b = record.b,
c = record.c
}).Where(record => record.z.Equals(35)).Distinct();
But when I remove column z from the table object in that fashion I get the following exception;
Binding error: Member 'table.z' not found in projection.
I can't return field z because it will render my distinct useless. Any help is appreciated, thanks.
Edit:
This is a more comprehensive example that includes the use of PredicateBuilder,
var clause = PredicateBuilder.False<User>();
clause = clause.Or(user => user.z.Equals(35));
foreach (int i in IntegerList) {
int tmp = i;
clause = clause.Or(user => user.a.Equals(tmp));
}
var results = (from u in db.Users
select new User {
a = user.a,
b = user.b,
c = user.c
}).Where(clause).Distinct();
Edit2:
Many thanks to everyone for the comments and answers, this is the solution I ended up with,
var clause = PredicateBuilder.False<User>();
clause = clause.Or(user => user.z.Equals(35));
foreach (int i in IntegerList) {
int tmp = i;
clause = clause.Or(user => user.a.Equals(tmp));
}
var results = (from u in db.Users
select u)
.Where(clause)
.Select(u => new User {
a = user.a,
b = user.b,
c = user.c
}).Distinct();
The ordering of the Where followed by the Select is vital.
problem is there because you where clause is outside linq query and you are applying the where clause on the new anonymous datatype thats y it causing error
Suggest you to change you query like
(from record in db.table
where record.z == 35
select new table {
a = record.a,
b = record.b,
c = record.c
}).Distinct();
Can't you just put the WHERE clause in the LINQ?
(from record in db.table
where record.z == 35
select new table {
a = record.a,
b = record.b,
c = record.c
}).Distinct();
Alternatively, if you absolutely had to have it the way you wrote it, use .Select
.Select(r => new { a = r.a, b=r.b, c=r.c }).Distinct();
As shown here LINQ Select Distinct with Anonymous Types, this method will work since it compares all public properties of anonymous types.
Hopefully this helps, unfortunately I have not much experience with LINQ so my answer is limited in expertise.
I have linq query as follows:
var result = (from Customer cust in db select new { userNameList = cust.UserName }).ToList();
i want to loop through each value in the list<>
I tried to use the foreach to accomplish this. It is stupid i could not figure it out
I'm using something like this
foreach (List<string> item in result)
{
if (item.ToString() == userName)
{
userExistsFlag = 1;
}
}
But the .net compiler is just freaking out:
and giving me these errors
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'
Cannot convert type 'AnonymousType#1' to 'System.Collections.Generic.List'
Thanks in anticipation
OF ALL THESE IMPLEMENTATIONS WHICH ONE IS MOST EFFICIENT AND CONSUMES LESS RESOURCES.
IT WOULD BE KIND ENOUGH IF SOME ONE CAN CLARIFY THIS FOR ME.
Shorter using Linq:
bool userExistsFlag = result.Any( x=> x.userNameList == userName);
As suggested in the other answers you do not need to project to an anonymous type:
var userNames = (from Customer cust in db select cust.UserName).ToList();
bool userExists = userNames.Contains(userName);
Edit:
The most efficient - if you do not need the set of user names otherwise - is to query the DB directly to check whether the user name exists, so
bool userExists = db.Any( x => x.UserName == userName);
Credit goes to #Chris Shaffer in the comments and #Cybernatet's answer - he was almost there. I would suggest you accept his answer but use Any() ;-)
Try:
var result = (from Customer cust in db select new { userNameList = cust.UserName }).ToList();
userExistsFlag = result.Where(a=> a.userNameList == userName).Count() > 0;
or
userExistsFlag = (
from Customer cust in db
where cust.UserName = userName
select cust
).Count() > 0;
If your query returns a list of names, your FOREACH loop should look like this
foreach( String name in results ){
...
}
Skip using new { userNameList = cust.UserName } which is making it an anonymous instance. You can try
var result = (from Customer cust in db select cust.UserName ).ToList();
if you're just getting the one property and want a list of strings there is no reason to use an anonymous type. code should work like this:
var result = (from Customer cust in db select cust.UserName).ToList();
How can I do this SQL query with Entity Framework?
SELECT DISTINCT NAME FROM TestAddresses
Using lambda expression..
var result = EFContext.TestAddresses.Select(m => m.Name).Distinct();
Another variation using where,
var result = EFContext.TestAddresses
.Where(a => a.age > 10)//if you have any condition
.Select(m => m.name).Distinct();
Another variation using sql like syntax
var result = (from recordset
in EFContext.TestAddresses
.where(a => a.city = 'NY')//if you have any condition
.select new
{
recordset.name
}).Distinct();
Try this:
var results = (from ta in context.TestAddresses
select ta.Name).Distinct();
This will give you an IEnumerable<string> - you can call .ToList() on it to get a List<string>.
The way that #alliswell showed is completely valid, and there's another way! :)
var result = EFContext.TestAddresses
.GroupBy(ta => ta.Name)
.Select(ta => ta.Key);
I hope it'll be useful to someone.
DBContext.TestAddresses.Select(m => m.NAME).Distinct();
if you have multiple column do like this:
DBContext.TestAddresses.Select(m => new {m.NAME, m.ID}).Distinct();
In this example no duplicate CategoryId and no CategoryName i hope this will help you
Entity-Framework Select Distinct Name:
Suppose if you are using Views in which you are using multiple tables and you want to apply distinct in that case first you have to store value in variable & then you can apply Distinct on that variable like this one....
public List<Item_Img_Sal_VIEW> GetItemDescription(int ItemNo)
{
var Result= db.Item_Img_Sal_VIEW.Where(p => p.ItemID == ItemNo).ToList();
return Result.Distinct().ToList();
}
Or you can try this Simple Example
Public Function GetUniqueLocation() As List(Of Integer)
Return db.LoginUsers.Select(Function(p) p.LocID).Distinct().ToList()
End Function
use Select().Distinct()
for example
DBContext db = new DBContext();
var data= db.User_Food_UserIntakeFood .Select( ).Distinct();
In order to avoid ORDER BY items must appear in the select list if SELECT DISTINCT error, the best should be
var results = (
from ta in DBContext.TestAddresses
select ta.Name
)
.Distinct()
.OrderBy( x => 1);
Entity-Framework Select Distinct Name:
Suppose if you are want every first data of particular column of each group ;
var data = objDb.TableName.GroupBy(dt => dt.ColumnName).Select(dt => new { dt.Key }).ToList();
foreach (var item in data)
{
var data2= objDb.TableName.Where(dt=>dt.ColumnName==item.Key).Select(dt=>new {dt.SelectYourColumn}).Distinct().FirstOrDefault();
//Eg.
{
ListBox1.Items.Add(data2.ColumnName);
}
}