LINQ Join on Multiple smallint Columns - c#

i am tying to join multiple columns. There is no problem if column type int or string etc.. but my columns types are smallint.
query:
var getworks = (from loc in db.T_location
join wl in db.T_vehicle_work_list
on new {x=loc.Route_id, y=loc.Cash_center_num}
equals new { x=wl.Route_id, y=wl.Cash_center_num}
where wl.Route_id == getVehicleRouteId.Route_id && wl.Cash_center_num == getVehicleRouteId.Cash_center_num
&& wl.Status_code != "C"
&& wl.Instance_id > bfd
&& wl.Instance_id < afd
select new { loc, wl }).ToList();
error : "The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'."
thanx for help...

If anybody have this problem too. Check the model.edmx column "nullable" properties are same

Related

Join with Where Clause in LINQ

I've got two tables that I'm trying to join together with an ID, but only select rows from Table A, where a value in Table B is null.
I tried this:
var dbXMLSoccerTeams = (from dbXMLSoccerTeam in data.EFXMLSoccerTeam
where dbXMLSoccerTeam.ID == (from dbMatchTeam in data.EFMatchingTeamIDs
where dbMatchTeam.SmarketsID == null
select dbMatchTeam.XMLSoccerID)
select dbXMLSoccerTeam);
But I get an error saying that operator == can not be used to compare int to iQueryable int
It seems to me that you should actually use a join:
var dbXMLSoccerTeams = from dbXMLSoccerTeam in data.EFXMLSoccerTeam
join dbMatchTeam in data.EFMatchingTeamIDs
on dbXMLSoccerTeam.ID equals dbMatchTeam.XMLSoccerID
where dbMatchTeam.SmarketsID == null
select dbXMLSoccerTeam;
Try:
var dbXMLSoccerTeams = (from dbXMLSoccerTeam in data.EFXMLSoccerTeam
from dbMatchTeam in data.EFMatchingTeamIDs
where dbMatchTeam.SmarketsID == null
&& dbXMLSoccerTeam.ID == dbMatchTeam.XMLSoccerID
select dbXMLSoccerTeam)

Conversion of SQL into LINQ

I need help converting some an SQL select into LINQ
Here is the original SQL:
Select Top 1 IsNull(ct.Template, t.Template) as Template
From Template t
Left Outer Join ClientTemplate ct On t.TemplateTypeId = ct.TemplateTypeId And ct.ClientId = 149
Where t.TemplateTypeId = (Select TemplateTypeId From QuoteType Where QuoteTypeId = 7)
Order By t.Version DESC, ct.Version DESC
I'm using Entity Framework and have entities for QuoteTypes, ClientTemplates and Templates.
The above SQL gets the Template from the ClientTemplate table for client 149 (uses a variable in the real code) for a particular QuoteType. If there is no entry in the CLientTemplate table then it returns the Template from the main Template table for the same QuoteType!
My idea was to query the QuoteType first and then query the ClientTemplate table to see if one exists and if not query the Template table instead. The problem is this will result in three queries but I'm sure it can be done in one swoop!?
Can anyone have a go at writing the LINQ for me?
Here's my mess so far:
QuoteType quoteType = (from qt in this.entities.QuoteTypes where qt.QuoteTypeID == this.SelectedNewQuoteTypeID select qt).First();
if (quoteType != null && quoteType.TemplateTypeID.HasValue)
{
int quoteTypeTemplateTypeID = (int)quoteType.TemplateTypeID;
var query = (from t in this.entities.Templates
join ct in this.entities.ClientTemplates on t.TemplateTypeID equals ct.TemplateTypeID
into a
from b in a.DefaultIfEmpty(new ClientTemplate())
where t.TemplateTypeID == quoteTypeTemplateTypeID
orderby t.Version descending
select new
{
T1 = t.Template1,
T2 = b.Template
}).First();
// Check the query to see if T1 and T2 are null and use whichever one isn't!
// TODO !!!
}
else
{
return string.Empty;
}
I kind of gave up once I got that far and posted this! My example still does two queries and does not select based on the client ID. It also does not have the second order by on the client template table.
I've inherited the original SQL statement so maybe the problem is with that being badly written in the first place!?
Over to you...
I haven't tested it but maybe you could try something like this
from t in this.entities.Template
from ct in this.entities.ClientTemplate.Where(x => t.TemplateTypeId == ct.TemplateTypeId && ct.ClientId == 149).DefaultIfEmpty()
where t.TemplateTypeId == (from x in this.entities.QuoteType where x.QuoteTypeId == 7 select x.TemplateTypeId).FirstOrDefault()
orderby t.Version descending, ct.Version descending
select new { ct.Template == null ? t.Template : ct.Template }
I think this might work, although this is untested, from reading it appears you can't add multiple join conditions to a LinQ statement so you can just specify it in the where clause
So here ya go, I tried to convert the SQL word for word so here's my attempt. If it does everything I think it will do it will work.
int templateTypeId;
templateTypeId= (context.QuoteType.Where(x => x.QuoteTypeId == 7)).FirstOrDefault().TemplateTypeId
var qry =(
from t in context.Template
join ct in context.ClientTemplate on
t.TemplateTypeId equals ct.TemplateTypeId into cts
from ct in cts.DefaultIfEmpty() //left join
where t.TemplateTypeId == templateTypeId
&& ct.ClientId == 149
order by t.Version descending, ct.Version descending
select new
{
Template = (ct.Template != null) ? ct.Template : t.Template //ternary operator
}).FirstOrDefault();

LINQ To DataSet from SQL

I am having trouble converting a SQL Join statement into LINQ to DataSet. Also, consider that there will be other tables in the LINQ statement that the Header table will be joined to. Below is my Join statement - any help is appreciated.
FROM Header
LEFT JOIN Address
ON Header.Customer = Address.Customer
AND Header.Company = Address.Company
AND ((Header.ShipTo = 'TEMP' AND Header.DocNum = Address.ShipTo)
OR Header.ShipTo <> 'TEMP' AND Header.ShipTo = Address.ShipTo)
Doing a join in Linq uses the Equals() method. When using multiple columns, you'll have to create an identically structured anonymous type for comparison.
from h in db.header
join a in db.address
on new {
cust = h.Customer,
comp = h.Company
}
equals new
{
cust = a.Customer,
comp = a.Company
}
where ((h.ShipTo == "TEMP" && h.DocNum == a.ShipTo)
|| h.ShipTo != "TEMP" && h.ShipTo == a.ShipTo)
select h;

Get Count from one value in three table linq2sql select?

I just want the apartment complex count along with the other values. Only adding the count breaks the code. The error I get is "Sequence operators not supported for type 'System.String'." I have also tried changing apartCount to an int with no luck. Any help would be appreciated
using (var db = new DataClasses2DataContext())
{
var zips = (from s in db.ZipCodeServiceAvailabilities
join b in db.ZipCodeBoundaries on s.ZipCode equals b.ZipCode
join a in db.pdx_apart_views on s.ZipCode equals a.Zip_Code
where (s.IsServiced == 1 && b.Ordering % 10 == 0)
orderby b.ZipCode
select new
{
zipCode = b.ZipCode.Trim(),
latitude = b.Latitude,
longitude = b.Longitude,
apartCount = a.Apartment_complex.Count()
}).ToArray();
}
I think you miss group by clause in your query.
or you can use corolated sub query in select clause. please explain your question more

T-SQL to LINQ conversion

I have the following SQL statement
SELECT
c.CorpSystemID, c.SystemName ,
case when a.TaskItemID is NULL then 'false' else 'true' end as Assigned
FROM CorpSystems c
LEFT OUTER JOIN
(SELECT CorpSystemID, TASKItemID
FROM AffectedSystems
where TASKItemID = 1) a ON c.CorpSystemID = a.CorpSystemID
Can anyone please help me to convert this statement to LINQ?
Thank you.
Ok so assume you've got a list of your CorpSystem objects in a variable called Corpsystems and a list of your AffectedSystem objects in a variable called AffectedSystems. Try the following:
Edit: For a join on all Affected Systems, try this:
var matches = from c in CorpSystems
join a in AffectedSystems on c.CorpSystemId equals a.CorpSystemId into ac
from subSystem in ac.DefaultIfEmpty()
select new
{
c.CorpSystemId,
c.SystemName,
Assigned = subSystem != null && subSystem.TaskItemId != null
};
Or for just AffectedSystems that have a TaskItemId of 1:
var matches = from c in CorpSystems
join a in AffectedSystems.Where(as => as.TaskItemId == 1)
on c.CorpSystemId equals a.CorpSystemId into ac
from subSystem in ac.DefaultIfEmpty()
select new
{
c.CorpSystemId,
c.SystemName,
Assigned = subSystem != null && subSystem.TaskItemId != null
};
See the answers to the following SO question SQL to LINQ Tool, assuming that you do not want to go through the process by hand.

Categories