Which Assembly contains LINQ extension method Delete? - linq

I am trying to delete a row from an entity named xyz in CRM using the following code in LINQPAD
var d = from z in xyz
where z.exch_ZipCode == "60069"
&& z.exch_zipcodeId.Value== new Guid("c6e88a07-b4a2-e211-b8d2-bc305befb465")
select new
{
zipId = z.exch_zipcodeId.Value,
zip = z.exch_ZipCode,
};
d.AsEnumerable().ToList().ForEach(row=>row.Delete());
I have added System.Data.DataSetExtensions.dll but I get the following error
'AnonymousType#1' does not contain a definition for 'Delete' and no extension method 'Delete' accepting a first argument of type 'AnonymousType#1' could be found (press F4 to add a using directive or assembly reference)

If this is LinqToSql then to delete rows you use DeleteOnSubmit or DeleteAllOnSubmit.
For example, if you want to delete all rows in xyz matching your criteria, you can do something like :
var query = (from z in xyz
where z.exch_ZipCode == "60069"
&& z.exch_zipcodeId.Value== new Guid("c6e88a07-b4a2-e211-b8d2-bc305befb465")
select z);
xyz.DeleteAllOnSumit(query);
SubmitChanges();

Related

LINQ - Join only one attribute with whole schema onf another entity

I have two entities smtracks and tracks_registration I write the following linq
var track = from tracksData in sqlEntities.smtracks
join track_registrationData in sqlEntities.tracks_registration on tracksData.TracksID equals track_registrationData.FkTrackId
where tracksData.TracksID == 35 && track_registrationData.TuneCode == 5982234E
select new
{
Tunecode = track_registrationData.TuneCode
};
now i just need this one attribute Tunecode from the other table and rest i want to have the whole schema of smtracks selected. The only way i know is that I select all the attributes in the LINQ above like
select new
{
Tunecode = track_registrationData.TuneCode
TracksID = smtracks.TracksID
SongTitle = smtracks.SongTitle
.
.
.
.
.
};
and there are a lot of attributes, isn't there a simpler way to select the entire schema of one table and the just one attribute from other.
If you don't mind have extra parameter to hold the smtracks, then you can write like this :-
select new
{
smtracks=tracksData ,
Tunecode = track_registrationData.TuneCode
};
Later on you can get the value like smtracks.TracksID and so on.

Avoid "Parameterless Queries Error" with Linq Let clause

I'd like my Linq query to create an additional column in the results on the fly. In this case the column is a Class object I created that will contain image info. I was wondering what the right way is of doing this:
var validPics = (from x in db.picsVotesTagsJs let picObj = new CarShowImages(x.picname) where x.enabled == 1 select x).Take(25);
var myArray = validPicSummaries.ToArray();
Line 2 gerenates the error:
Only parameterless constructors and initializers are supported in LINQ to Entities.
This is my first time using the Let clause. My queries are usually pretty simple.
Create parameterless constructor and use some public property (e.g. PicName) to set picture name to your CarShowImages object:
var validPics = (from x in db.picsVotesTagsJs
where x.enabled == 1
select new CarShowImages { PicName = x.picname }).Take(25);
var myArray = validPics.ToArray();

retrieve all areas where id's don't exist in supplied list

I'm sure I must be missing something really simple here..
OK I have a list of AreaIds. I want to compare that list to the MapArea Table and return any IDs that exist in the table but NOT in the supplied list.
This is my list of supplied areas that I want to check:
var currentAreas = (from c in _entities.mapAreaLink
where c.listingId == id
select new
{
c.MapArea.areaId
}
).ToList();
This is the getting the exhaustive list of mapAreas..
var availableAreas = (from m in _entities.MapAreas
select new
{
m.areaId
}
).ToList();
This compares the two lists and gets items that exist in the maparea table but not in the maparealink (constrained by an id of the item I am looking at).
var unusedAreas = availableAreas.Except(currentAreas).ToList();
I seem to get the list back ok, but what I need to do is now return a list of maparea objects based on the results of the Except.tolist above.
I thought I could do this:
var mapareas = (from e in _entities.MapAreas
where unusedAreas.Contains(e.areaId)
select e).ToList();
I am getting an ambiguous invocation on the where & "Cannot resolve method Contains(int)" on the e.areaId.
Ive tried using:
var unusedAreas = availableAreas.Except(currentAreas).ToArray();
No Joy.. Can anyone help me out here - I am guessing I must be missing a fundamental basic here.
many thanks
You create anonymous types with just one int property. That's not necessary and it causes the later problem. If you create lists of int you'll be OK:
var currentAreas = (from c in _entities.mapAreaLink
where c.listingId == id
select c.MapArea.areaId).ToList();
var availableAreas = (from m in _entities.MapAreas
select m.areaId).ToList();

Linq - Updating all the properties but not the key one

I've an 'product' object that has to update a record in db.
Through a 'productCode' I retrieve the object to update using Linq.
Which is the most elegant way to overwrite alle the property but not the key one and then save changes?
You will need to access each property anyways, as you fetch a record manually. my recommendation for easiest, fastest and cleanest code:
code = productCode_toChange
using(Entity ent = new Entity())
{
var update = (from x in ent.Products where x.productCode == code select x).First();
update.property1 = product.property1;
update.property2 = product.property2;
// and so on for each property you change
ent.SaveChanges();
}
you could of course try:
using(Entity ent = new Entity())
{
var update = (from x in ent.Products where x.productCode == code select x).First();
update = product;
ent.SaveChanges();
}
but i can almost guarantee this will not work, as product will certainly have an id-property it will try to write to update, which will throw an exception, given the case product is the LINQ-generated type for the table-instance.
Be careful to check for type-conformity. also note, that you the SQL-Type of product code should not be text, as this type is incomparable

How to write LINQ IN clause query which will work as LIKE operator as well?

How we can write a LINQ query for following select sql query:
string brandid="1,2,3"
string bodystyleid="1,2,3"
-------------------
-----------------
select * from car
where brandid in (brandid)
and bodystyleid in (brandid)
----------------------
-------------------
My specific requirement is that if brandid or bodystyleid is blank(if user does not select
any checkbox of a particular search option) query should return all record for that particular where condition.
Please guide me.
Thanks,
Paul
In order to fulfil your requirement about returning all items if none are specified, you need to check for the lists being empty.
var brands = brandid.Split(',').Select(x => Int32.Parse(x));
var styles = bodystyleid.Split(',').Select(x => Int32.Parse(x));
var result = from c in car
where (!brands.Any() || brands.Contains(c.brandid))
&& (!styles.Any() || styles.Contains(c.bodystyleid))
select c;
(similar to sgmoore's solution, but includes the check for no brand/style specified)
I've not actually checked how this gets converted back to SQL - it may be more efficient to use a flag to indicate whether there are any values:
var brands = ....; // As above
bool anyBrands = brands.Any()
var result = from c in car
where (!anyBrands || brands.Contains(c.brandid))
.....
Is bodystyleid meant to check brandid or bodystyleid? (I am assuming bodystyleid, however have wrote the query to match the query in the question (brandid))
As a start you could do:
var results = (from c in car
where c.brandid.Contains(brandid)
&& c.bodystyleid.Contains(brandid)
select c).ToList();
var brandids = brandid .Split(',').Select(n => int.Parse(n)).ToList();
var bodyStyleids = bodystyleid.Split(',').Select(n => int.Parse(n)).ToList();
var results =
(from c in car where
brandids.Contains(c.brandid) &&
bodyStyleids.Contains(c.bodystyleid)
select c
).ToList();
the Ids you have are as strings with comma delimiter, you need them to be collections like List of the same type as your Ids of the Car table, so if brandid column is int then brandids has to be List<long>, then you can do
var results = (
from c in cars
where brandids.Contains(c.brandid) && bodystyleid.Contains(c.bodystyleid)
select c).ToList();

Resources