collation conflict - linq

Is there any one who knows how we can solve collation issue in select linq query?
I'm getting this error when I want to select data in linq.
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation
var lstData = from s in dataTrackDB.datas
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join m in dataTrackDB.mktDatas on s.mktcode equals m.mktcode
select new dataView {
Account=m.account,
brandcode=b.brandcode,
commodity=s.commodity,
date=s.date,
daysvalid=s.daysvalid,
mfrcode=b.mfrcode,
mktcode=s.mktcode,
price=s.price,
prodid=s.prodid,
statecode=s.statecode,
subcommodity=s.subcommodity,
supprecode=s.supprecode,
units =s.units
};
lstData = lstData.AsQueryable().Where(x => x.mfrcode == mfr );
return lstData.Take(100).ToList();

The problem is not in Linq but in your database
you can for example create a view that joins that way and the select the data in linq from the view
SELECT * FROM T1
INNER JOIN T2 ON
T1.Name COLLATE Latin1_General_CI_AS = T2.Name COLLATE Latin1_General_CI_AS
or select the data first in linq2sql separately for each table and then join it with linq2object

add COLLATE DATABASE_DEFAULT at the end of the query

Related

Join to another table when the join column is null

I have a sql that is selecting many things from the database however I would like that data to only comeback which is matched to a personal table I have.
I would like to join a column [vin_code] from my table [population] however there are nulls in here and were there are nulls I would like to join another column from my table to another table in the database.
I will give an example sql below:
Select distinct v.kegal_rntity_id
From vin v
Inner join ops$dami.population pop
On v.vin_code = pop.vin_code
Then were pop.vin_code is null I would like to join pop.vis_code on a table in the database called zegal_rentity z column z.vis_code
So something like
join zegal_rentity z
On pop.vis_code = z.vis_code
But I only want to do this were pop.vin_code is null
As sample data is not available, I am unable to test the solution but try the following query with condition based outer join.
Select distinct v.kegal_rntity_id
From ops$dami.population pop
Left join vin v
On v.vin_code = pop.vin_code
Left join zegal_rentity z
On (case when pop.vin_code is null and
pop.vis_code = z.vis_code then 1 end = 1);
Cheers!!

Need to Convert this SQL query to LINQ

can anybody help me to convert this SQL query to LINQ code in MVC? I need to return a list. The DB context entity is: _dbContext.
select distinct table1.AIG_ID, table1.GMT_NAME, table1.AIG_Number
from table1 left join table2 on table1.AIG_ID = table2.AIG_ID**
var data=(from item in db.table1 join
item1 in db.table2 on item.AIG_ID equals item1.AIG_ID
select new {item.AIG_ID ,item.GMT_NAME ,item.AIG_Number }).GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault()).ToList();
I write this part for your distinct in sql
GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault())

Select value from grandchildren

I have the following table structure:
I want to select:
all TableA entries + the Identifier column from Table C that have:
a special value in TableBType ("TableBTypeValue")
a special value in TableCType ("TableCTypeValue")
The problem that I have is that the linq queries seem to fail when there are TableA entries that have no TableB entry or if there are TableB entries without a TableC (TableBType and TableCType is mandatory so they don't have that problem).
With SQL this would not be a big problem, but as I am new to linq I could not find the correct way to create this query.
I think this is what you are looking for:
from c in db.TableC
where c.TableCType == TableCTypeValue
join b in db.TableB on c.TableBId equals b.Id
where b.TableBType == TableBTypeValue
join a in db.TableA on b.TableAId equals a.Id
select new { a, c.Identifier };
Hope it helps.

entity framework - filtering query with nullable join keys

I have a query based on the following T SQL:
select t1.field1
t2.field1,
t2.field2,
t3.field1,
t3.field2
from t1 left outer join t2 on t1.t2key = t2.id
left outer join t3 on t1.t3key = t3.id
In Linq to Entities the query takes the form
var query = db.context.t1.include(“t2”).include(“t3”);
The table t1 has an unusual structure in that fields t2key and t3key are nullable. The nullable fields are preventing me from filtering by any of the t2 or t3 fields.
The only way out I can see so far is to return the results as a database view before I perform the filtering. Or is there another approach I should be taking here?
For what it's worth I ended up using Linq in the end. If anyone else is in the same boat this is the syntax that got around the nullable issue:
query = query.Where(x => x.t1field != null ? x.t1field.t3field.Contains(model.propvalue) : false).Select(x => x);

How to do a "join" in a LINQ query against the Entity Framework

I have the following table structure which has been imported into the Entity Framework. I need to write a LINQ query where I select the entities of Table1, where a field in Table2 is equal to true, and a field in Table 3 is equal to a specific GUID.
Could someone help with this?
Thanks you.
alt text http://digitalsamurai.us/images/drawing2.jpg
Try:
from t3 in dataContext.Table3
where t3.Guidfield == someGuid
from t2 in t3.Table2
where t2.Field // boolean field is true
select t2.Table1;
EDIT: As requested, equivalent lambda expression syntax:
dataContext.Table3.Where(t3 => t3.Guidfield == someGuid)
.SelectMany(t3 => t3.Table2)
.Where(t2 => t2.Field)
.Select(t2.Table1);
from t1 in table1
join t2 in table2
on t1.table1PK equals t2.table1PK
join t4 in table4
on t2.table2PK equals t4.table2PK
join t3 in table3
on t3.table3PK equals t4.table3PK
where t2.randomBoolColumn == true && t3.GUID == myGUIDVariable
select t1;

Resources