LINQ .Count() not defined for int - linq

I have the below LINQ statement, and get the error " 'int' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'int' could be found ( are you missing a using directive or assembly reference?) "
var queryFuture = from pqv in context.OrderPrintQueue_View
join od in context.Order_Details on pqv.confirmId equals od.ConfirmId
join pp in context.Product_Price on od.priceId equals pp.priceId
join p in context.Products on pp.productId equals p.ProductID
select new { p.stationId, inProc = 0, OrderLinesCount = od.recId.Count() };
od.recId.Count() is the portion that is returning an error. I'm very new to LINQ (have been using it for about 2 days) and am a novice programmer. The answers I have found all say to include the system.core assembly reference, and of course, the System.Linq using. I have all of those so I'm not sure what the deal is. I am using WPF with .NET 4, EF, and RIA services and the MVVM pattern.

recID is of type int, not IEnumerable<?>. Count() is only defined on IEnumerable<?>. You will ned a group by statement:
var queryFuture = from pqv in context.OrderPrintQueue_View
join od in context.Order_Details on pqv.confirmId equals od.ConfirmId
join pp in context.Product_Price on od.priceId equals pp.priceId
join p in context.Products on pp.productId equals p.ProductID
group od by od.recId into orders
select new { p.stationId, inProc = 0, OrderLinesCount = orders.Count() };
Note: I'm not sure of this combination of group by and join will work out, as I usually only use the method chains. You might have to adjust on the operators, however you'll need a group by in any case.

Why would you want to get the count for an integer? You are probably getting a single value back, not a collection, which it seems is what you are expecting.

Related

Performing two Left Outer Joins in a Single LINQ to CRM query

I'm attempting to perform two left outer joins in my CRM online 2015 Update 1 instance, but I am getting an error. This is what I have currently:
var query_join8 = from a in crmContext.AccountSet
join c in crmContext.ContactSet
on a.PrimaryContactId.Id equals c.ContactId
into gr
from c_joined in gr.DefaultIfEmpty()
join c in crmContext.ContactSet
on a.Name equals c.FullName
into gr2
from c2_joined in gr2.DefaultIfEmpty()
select new
{
contact_name = c_joined.FullName,
account_name = a.Name,
other_name = c2_joined.FullName
};
When I attempt to execute it, I get this error:
An exception of type 'System.NotSupportedException' occurred in
Microsoft.Xrm.Sdk.dll but was not handled in user code
Additional information: The method 'GroupJoin' cannot follow the
method 'SelectMany' or is not supported. Try writing the query in
terms of supported methods or call the 'AsEnumerable' or 'ToList'
method before calling unsupported methods.
If I comment out the second Join, it works fine:
var query_join8 = from a in crmContext.AccountSet
join c in crmContext.ContactSet
on a.PrimaryContactId.Id equals c.ContactId
into gr
from c_joined in gr.DefaultIfEmpty()
//join c in crmContext.ContactSet
//on a.Name equals c.FullName
//into gr2
//from c2_joined in gr2.DefaultIfEmpty()
select new
{
contact_name = c_joined.FullName,
account_name = a.Name,
//other_name = c2_joined.FullName
};
Microsoft Documentation:
Defining how to perform a Left Join: http://msdn.microsoft.com/en-us/library/gg509017.aspx#LeftJoin
Blog Describing that it is supported: http://blogs.msdn.com/b/crminthefield/archive/2013/01/14/crm-2011-sdk-query-limitations-by-api.aspx
The documentation on CRM 2015 still states outer joins are not supported. (MSDN: Use LINQ to construct a query) I know there is also an example on MSDN showing a left join (Sample: Complex LINQ queries), but I doubt if under the hood this is actually transformed into a single QueryExpression. So, I guess you are hitting the limits of the capabilities of Ling for CRM.

Problems getting System.Linq.Dynamic to work.

I have included the System.Linq.Dynamic library in my project via Nuget. The following is my linq query which works fine if I use typed fields to be returned in the select. But using System.Linq.Dynamic I should be able to use a string value for the select. I've followed the examples that I found but all I get back from the select is the string itself.
What am I missing?
var predicate = PredicateBuilder.False<Name>();
predicate = predicate.And(d => d.ID == "100053");
var results = from n in Names
.AsExpandable()
.Where(n=> n.ID=="100053")
join d in InstitutionDemographics on n.ID equals d.ID
join m in MemberAdhocIds on n.ID equals m.ID
join a in NameAddresses on n.BillingAddressNumber equals a.AddressNumber
join mas in MemberAdhocServices on n.ID equals mas.InstitutionID
select("new(n.ID,n.Company,n.MemberStatus,n.Email,n.MemberType,n.USCongress,n.FAX,n.County,d.NumberYearsAMember,d.Population,d.FederalReserveDistrict,d.FDICCertificateNumber,d.FRSID,d.ICBADistrictCode,d.UD_Minority_Type,d.MSA,d.NumberOfBranches,d.PubliclyTraded,d.SRAMemberships,d.Assets,d.RSU,d.FutureDues,d.InstitutionType,d.AgLoanPercentageTotal,m.CCRP,a.City,a.State,a.Address1,a.ZIP)");
results.Dump();
If you're telling the select statement to select a string, it's not going to return the values for those strings. What happens if you try without the quotes?
I'm not fluent in C#, being a VB fiend, but you could try something like:
Select New With {.Id = n.ID, .Company = n.Company, ...etc}
Does that make sense? Apologies if I've misunderstood your question.
My mistake. I didn't realize how the extension worked. I was expecting the extension to work in query syntax when in reality it would only work in method syntax.
i.e.
.Select("new (ID,Name)");
(this works)
-vs-
select(new(ID,Name)");
(this doesn't work)

LINQ/LinqPad: same query different results

So we copy and paste the exact same query from LinqPad into our EF 4.3 application, pointed at the exact same database and get a different result. In LinqPad we get 2 records returned. In our application we reaise an error "Object reference not set to an instance of an object."
var Shippings = shippingRepository.All.ToArray();
var SalesOrderHeaders = salesOrderHeaderRepository.All.ToArray();
var Customers = customerRepository.All.ToArray();
var Stores = storeRepository.All.ToArray();
var Departments = departmentRepository.All.ToArray();
var toShip = from sh in Shippings
join h in SalesOrderHeaders on sh.OrderId equals h.SalesOrderHeaderId
join c in Customers on h.CustomerId equals c.CustomerId
join st in Stores on h.StoreId equals st.StoreId
join d in Departments on h.DepartmentId equals d.DepartmentId into outer
from o in outer.DefaultIfEmpty()
select new
{
OrderId = sh.OrderId,
CustomerName = c.Name,
StoreName = st.Name,
DepartmentName = (o.Name == null) ? o.Name : "None",
DeliveryDate = h.DeliveryDateTime
};
In the application code, when we remove the outer join (to add Departments) and it's associated field the query returns the same 2 records asn in LinqPad.
Does anyone have any insight into how to fix this feature?
Click on "Add a connection" in linqpad and select datacontext from assembly like
You can choose Entity Framework datacontext or Entity Framework BDContext with POCO depending upon your scenario. click next and provide path to the assembly along with connection string and you will be good to go.
In LINQPad are you actually querying against your entity model? Take a look at this link if you aren't. I had a similar problem when starting out and didn't realize I had set up a default LINQ to SQL connection earlier and was querying against that.

Using .Contains within a linq query returning a SystemException

I am having some trouble with a linq query I am trying to write.
I have List A of products that have been modified so I am trying to get the list of products from the db to allow me to apply the changes to them.
I have tried 2 different queries
var query = from p in db.Products
where products.Select(z => z.id).Contains(p.Id)
select p;
var query2 = from p in db.Products where (from o in products
select o.id)
.Contains(p.Id)
select p;
Both attempts are returning an error
base {System.SystemException} = {"Unable to create a constant value of type 'ProjectABC.Models.ProductModel'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."}
What am I doing wrong?
I had the same problem the other day, seems EF doesn't support Select().Contains() without giving that error. After testing around for a bit, I ended up splitting it up in what in your case would correspond to;
var IDs = products.Select(z=>z.id);
var query = from p in db.Products
where IDs.Contains(p.Id)
select p;
which worked well in my case when the "products" collection was in memory anyway (ie a ToList()'ed result from the database)

LINQ query complex join problem

So I was trying to convert a SQL into LINQ query
the logic is:
JOIN SalesPeriod SP1
ON
SP1.SalesPeriodId = SE1.SalesPeriodId AND SP1.SalePeriodId = .....(XML stuff)
but it keeps complaining the types on both sides of equals statement don't match
Any ideas?
Note: I declared b and d because it doesn't accept anonymous type members
and I tested two equal conditions separately and they both work
thanks
join SP1 in fentities.SalesPeriods
on new { SE1.SalesPeriodId, b = XDocument.Load(MI.Body).Element("ns:Transfer").Element("ns:ReceivedBy").Element("ns:Id").FirstNode.ToString() }
equals new { SP1.SalesPeriodId, d = SP1.SalesPeriodId.ToString() }
Simple, they're not the same (compatible) types. The first key has a SalesPeriodId of type whatever, and b of type string. The second key has a SalesPeriodId of type whatever (probably the same as the first's), and d of type string. You can't compare these to eachother. It must have the same properties of the same types declared in the same order. Just pick one of the names b or d and use that name.
...
join SP1 in fentities.SalesPeriods
on new { SE1.SalesPeriodId, b = XDocument.Load(MI.Body).Element("ns:Transfer").Element("ns:ReceivedBy").Element("ns:Id").FirstNode.ToString() }
equals new { SP1.SalesPeriodId, b = SP1.SalesPeriodId.ToString() }
Your two anonymous types do not match, b & d to be specific.. try aligning the signatures..
join SP1 in fentities.SalesPeriods
on new { SE1.SalesPeriodId, b = XDocument.Load(MI.Body).Element("ns:Transfer").Element("ns:ReceivedBy").Element("ns:Id").FirstNode.ToString() }
equals new { SP1.SalesPeriodId, b = SP1.SalesPeriodId.ToString() }
In that example both anonymous objects will have the same property definitions (SalesPeriodId and b)
Don't stuck on putting complete join condition into 'ON' clause. Split condition on two parts, put one of them into 'ON' clause and another into 'WHERE' clause.
join SP1 in fentities.SalesPeriods
on SE1.SalesPeriodId equals SP1.SalesPeriodId
where XDocument.Load(MI.Body).Element("ns:Transfer").Element("ns:ReceivedBy").Element("ns:Id").FirstNode.ToString() == SP1.SalesPeriodId.ToString()

Resources