I have a simple query where I want to get the attributes distinct using the value stored in the "Attribute" property. For some reason I always get Distinct operation not supported for this overload error.
var nounTypes = from c in query
join cna in ics.CatalogNounAttributes on c.CatalogId equals cna.CatalogId
join nta in ics.NounTypeAttributes on cna.NounTypeAttributeId equals nta.NounTypeAttributeId
join nt in ics.NounTypes on nta.NounTypeId equals nt.NounTypeId
select new { NounTypeName = nt.Name, Attributes = nt.NounTypeAttributes.Distinct() };
I would also like to get the Count of each Attribute grouped by "Attribute" value where Attribute is a property on NounTypeAttributes table.
I believe you should simply say nt.Distinct.
var nounTypes = from c in query
join cna in ics.CatalogNounAttributes on c.CatalogId equals cna.CatalogId
join nta in ics.NounTypeAttributes on cna.NounTypeAttributeId equals nta.NounTypeAttributeId
join nt in ics.NounTypes on nta.NounTypeId equals nt.NounTypeId
select new { NounTypeName = nt.Name, Attributes = nt.Distinct() };
You do not need to use the Distinct.
Take a look at :
http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#distinct2
Related
I want to have join query from a table with a dictionary based on a common field, my query is:
var query = from c in db.Exp
join d in lstUniprotDic on c.UniID equals d.Key
select new
{
c.UniID,
IdentityPercent=d.Value.ToString(),
c.PrId,
c.SpotNo
}
but i got the following error
Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.
That pretty much says it all. You can't use the dictionary in your LINQ to SQL query except when using Contains.
Solution:
(from c in db.Exp where lstUniprotDic.Keys.Contains(c.UniID) select c).AsEnumerable()
join d in lstUniprotDic on c.UniID equals d.Key
select new
{
c.UniID,
IdentityPercent=d.Value.ToString(),
c.PrId,
c.SpotNo
}
I am not sure if the usage of lstUniprotDic.Keys in the LINQ to SQL query is actually working.
If not, try using this code instead:
var ids = lstUniprotDic.Keys.ToArray();
(from c in db.Exp where ids.Contains(c.UniID) select c).AsEnumerable()
join d in lstUniprotDic on c.UniID equals d.Key
select new
{
c.UniID,
IdentityPercent=d.Value.ToString(),
c.PrId,
c.SpotNo
}
So I have the following code:
string searchQuery = collection["query"];
var srmas = (
from SRMAs in db.SRMAs
join SRMAStatus in db.SRMAStatus on SRMAs.Id equals SRMAStatus.Id
join PurchaseOrders in db.PurchaseOrders on SRMAs.PONumber equals PurchaseOrders.PONumber
join Suppliers in db.Suppliers on PurchaseOrders.SupplierID equals Suppliers.SupplierID
join SRMADetails in db.SRMADetails on SRMAs.Id equals SRMADetails.SRMAId
where ids.Contains(SRMAs.Status)
&&
(
searchQuery.Contains(PurchaseOrders.suppliersOrderNumber)
||
searchQuery.Contains(SRMAs.PONumber.ToString())
)
select new
{
SRMAs.Id,
SRMAs.PONumber,
SRMAs.CreatedOn,
Suppliers.SupplierName,
SRMAStatus.StatusName,
PurchaseOrders.PODate, PurchaseOrders.suppliersOrderNumber
}
).ToList();
Where searchQuery is a string variable.
I have to actually use IN clause ofr PONumber and for that purpose I am using Contains which gives error mentioned in title. How do I check non String values?
you could give SqlFunctions.StringConvert a shot, it'll marry you to sql server and requires .Net 4+
searchQuery.Contains(SqlFunctions.StringConvert((decimal)SRMAs.PONumber))
the function seems a little twitchy, when I was spinning up a sample I had to convert my int to a decimal to avoid a Ambigious Invoication build error.
One apporach would be to convert searchQuery to the numeric datatype that the PONumber is, and you are all set.
EF 4 does not support ToString() Method on queries. Either you need to update it to EF6 or you can use SqlFunctions.StringConvert function as follows.
string searchQuery = collection["query"];
var srmas = (
from SRMAs in db.SRMAs
join SRMAStatus in db.SRMAStatus on SRMAs.Id equals SRMAStatus.Id
join PurchaseOrders in db.PurchaseOrders on SRMAs.PONumber equals PurchaseOrders.PONumber
join Suppliers in db.Suppliers on PurchaseOrders.SupplierID equals Suppliers.SupplierID
join SRMADetails in db.SRMADetails on SRMAs.Id equals SRMADetails.SRMAId
where ids.Contains(SRMAs.Status)
&&
(
searchQuery.Contains(PurchaseOrders.suppliersOrderNumber)
||
searchQuery.Contains(SqlFunctions.StringConvert((double)SRMAs.PONumber))
)
select new
{
SRMAs.Id,
SRMAs.PONumber,
SRMAs.CreatedOn,
Suppliers.SupplierName,
SRMAStatus.StatusName,
PurchaseOrders.PODate, PurchaseOrders.suppliersOrderNumber
}
).ToList();
I want to convert this query into linq, please help:
select
mr_ssample.objectid,
mr_ssample.stcode
from mr_ssample
inner join mr_wsample on mr_ssample.objectid = mr_wsample.objectid
And mr_ssample.stcode in( select stcode
from mr_wsample)
i tried this in C#
var query = from ssamp in marineEntity.MR_SSAMPLE
join wsamp in marineEntity.MR_WSAMPLE on ssamp.OBJECTID equals wsamp.OBJECTID && ssamp.stcode.contains(wsamp.stcode)
select new
{};
However, I could not access wsamp in contains, or I dont know the alternative of this.
give this a try,
var _result = from a in mr_ssample
join b in mr_wsample on a.objectid equals b.objectid
where (from c in mr_wsample select new {c.stcode})
.Contains(new {a.stcode})
select new {a.objectid, a.stcode}
I have a LINQ to Entities statement that joins two models on an AlphaGroupID, like this:
IEnumerable<ICD.ViewModels.HomeSearchViewModel> ICDList = (from a in ICDUnitOfWork.AlphaGroups.Find()
join e in ICDUnitOfWork.Alphas.Find()
on a.AlphaGroupID equals e.AlphaGroupID)
I need to join the two tables on AlphaGroupID, but I also need all of the ICDUnitOfWork.AlphaGroups regardless of whether or not they have a corresponding AlphaGroupID in ICDList. How can I accomplish this?
Use join into (it is same as GroupJoin):
var query = from a in ICDUnitOfWork.AlphaGroups.Find()
join e in ICDUnitOfWork.Alphas.Find()
on a.AlphaGroupID equals e.AlphaGroupID into g
select new { AlphaGroup = a, Alphas = g };
GroupJoin produces hierarchical result - for each item in outer sequence will be generated sequence of corresponding items in inner sequence (sequence could be empty).
im working with xml and linq.
I have 2 xml files both contain "ID" and "LANGUAGE"
I want to do a join based on where the both the ID and LANGUAGE are equal in both files
I have something like this:
var data=
from details in h_details.Descendants("ROW")
join inst in instance.XPathSelectElements("//Row")
on details.Element("ID").Value
equals inst.XPathSelectElement("Field[#Name=\'h_id\']").Value
and on details.Element("LANGUAGE").Value
equals inst.XPathSelectElement("Field[#Name=\'h_lang\']").Value
basically the "and" statement wont work, so how do i join based on 2 conditions?
Anonymous types to the rescue.
var data=
from details in h_details.Descendants("ROW")
join inst in instance.XPathSelectElements("//Row")
on new {
x = details.Element("ID").Value,
y = details.Element("LANGUAGE").Value
} equals new {
x = inst.XPathSelectElement("Field[#Name=\'h_id\']").Value,
y = inst.XPathSelectElement("Field[#Name=\'h_lang\']").Value
}
select ... ;
try union to get the both lists and join them