using ef4
iam trying to convert this sql to linq but could not
select s.IdSimcard ,s.Imei from dbo.SimCard s
where s.Imei not in (select distinct d.Imei from dbo.SimCard d inner join dbo.Configuracion c
on c.SinCard_IdSincard = d.IdSimcard
where c.Estado = 'Activo' )
So far I have this with the help of linqpad, I cant use linquer anymore for the activation code
(from s in SimCard where s.Imei.Contains( (from c in Configuracion
join d in SimCard on c.SinCard_IdSincard equals d.IdSimcard
where c.Estado == "Activo"
select new { d.Imei }).Distinct())
select new { s.IdSimcard, s.Imei })
I have read "Not in" dont supported in EF4, what woul be the equivalent?
i get this error in linqpad
'string.Contains(string)'
- Argument 1: cant convert de 'System.Linq.IQueryable' to 'string'
If I understand your question, you can use the Except(...) Extension method (in the System.Linq namespace) instead of Not Contains(...).
Related
I want to display two tables data using join in web api. I have tried with this code, but getting error - The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'. and here is my code
public List<tblEmpTask> GettblEmpTasks()
{
var q = (from n in db.tblEmpTasks
join c in db.tblEmployees on n.intEmpCode equals c.intEmpCode
select new
{
n.strTaskName,
n.dtStart,
c.strEmployeeName,
}).ToList();
return q;
}
Any solution will be helpful.
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.
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)
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.
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.