How to check for NullReferenceException in LINQ result - wpf

I have a LINQ query that works fine with EF in my DAL:
using (var mLEntities = new myLab02Entities1())
{
var test = from c in mLEntities.Chemicals
from u in c.Usages
select new
{
cChemID = c.Chem_ID,
c.Name,
c.Supplier,
c.Grade,
c.OrderNo,
c.BatchNo,
c.EntryDate,
c.CreatedBy,
cUser = u.Person.PersName,
uChemID = u.Chem_ID,
u.Study_ID,
u.UsedBy,
uUser = u.Person.PersName,
u.UseDate,
u.Project.StudyNo,
u.Project.ProjectName,
};
Usages is a Navigation property of Chemicals (1 chem : n usa), and Person and Projects are both Navigation properties of Usages (both 1 : 1).
Then I wanted to put this query in my Business Logic and wrote:
IList<ChemicalBDO> chemicalListBDO = chemListDAO.GetChemicalsListFromDB();
var test = from c in chemicalListBDO
from u in c.Usages
select new
{
cChemID = c.Chem_ID,
c.Name,
c.Supplier,
c.Grade,
c.OrderNo,
c.BatchNo,
c.EntryDate,
c.CreatedBy,
cUser = u.Person.PersName,
uChemID = u.Chem_ID,
u.Study_ID,
u.UsedBy,
uUser = u.Person.PersName,
u.UseDate,
u.Project.StudyNo,
u.Project.ProjectName
};
That is, I first query my EF DBcontext and get my Chemicals Entity, and than I write a query against this Chemicals Entity.
For me the strange thing is that the last Code throws a System.NullReferenceExeption, because u.Person and u.Project can be NULL. But the first query does not throw exceptions, what is the reason for that?
How can I handle the NULL reference in the select query, can I check for NULL with "if", "?" or "??" ?
Btw: I thought having the complex query in the DAL is no good programming practice, is that true or can I let the Code there?
Any help is appreciated.

This should fix this problem:
IList<ChemicalBDO> chemicalListBDO = chemListDAO.GetChemicalsListFromDB();
var test = from c in chemicalListBDO
from u in c.Usages
select new
{
cChemID = c.Chem_ID,
c.Name,
c.Supplier,
c.Grade,
c.OrderNo,
c.BatchNo,
c.EntryDate,
c.CreatedBy,
cUser = u.Person != null ? u.Person.PersName : string.Empty,
uChemID = u.Chem_ID,
u.Study_ID,
u.UsedBy,
uUser = u.Person != null ? u.Person.PersName : string.Empty,
u.UseDate,
u.Project != null ? u.Project.StudyNo : -1,
u.Project != null ? u.Project.ProjectName : string.Empty
};

Related

linq2db - server side bulkcopy

I'm trying to do a "database side" bulk copy (i.e. SELECT INTO/INSERT INTO) using linq2db. However, my code is trying to bring the dataset over the wire which is not possible given the size of the DB in question.
My code looks like this:
using (var db = new MyDb()) {
var list = db.SourceTable.
Where(s => s.Year > 2012).
GroupBy(s => new { s.Column1, s.Column2 }).
Select(g => new DestinationTable {
Property1 = 'Constant Value',
Property2 = g.First().Column1,
Property3 = g.First().Column2,
Property4 = g.Count(s => s.Column3 == 'Y')
});
db.Execute("TRUNCATE TABLE DESTINATION_TABLE");
db.BulkCopy(new BulkCopyOptions {
BulkCopyType = BulkCopyType.MultipleRows
}, list);
}
The generated SQL looks like this:
BeforeExecute
-- DBNAME SqlServer.2017
TRUNCATE TABLE DESTINATION_TABLE
DataConnection
Query Execution Time (AfterExecute): 00:00:00.0361209. Records Affected: -1.
DataConnection
BeforeExecute
-- DBNAME SqlServer.2017
DECLARE #take Int -- Int32
SET #take = 1
DECLARE #take_1 Int -- Int32
SET #take_1 = 1
DECLARE #take_2 Int -- Int32
...
SELECT
(
SELECT TOP (#take)
[p].[YEAR]
FROM
[dbo].[SOURCE_TABLE] [p]
WHERE
(([p_16].[YEAR] = [p].[YEAR] OR [p_16].[YEAR] IS NULL AND [p].[YEAR] IS NULL) AND ...
...)
FROM SOURCE_TABLE p_16
WHERE p_16.YEAR > 2012
GROUP BY
...
DataConnection
That is all that is logged as the bulkcopy fails with a timeout, i.e. SqlException "Execution Timeout Expired".
Please note that running this query as an INSERT INTO statement takes less than 1 second directly in the DB.
PS: Anyone have any recommendations as to good code based ETL tools to do large DB (+ 1 TB) ETL. Given the DB size I need things to run in the database and not bring data over the wire. I've tried pyspark, python bonobo, c# etlbox and they all move too much data around. I thought linq2db had potential, i.e. basically just act like a C# to SQL transpiler but it is also trying to move data around.
I would suggest to rewrite your query because group by can not return first element. Also Truncate is a part of the library.
var sourceQuery =
from s in db.SourceTable
where s.Year > 2012
select new
{
Source = s,
Count = Sql.Ext.Count(s.Column3 == 'Y' ? 1 : null).Over()
.PartitionBy(s.Column1, s.Column2).ToValue()
RN = Sql.Ext.RowNumber().Over()
.PartitionBy(s.Column1, s.Column2).OrderByDesc(s.Year).ToValue()
};
db.DestinationTable.Truncate();
sourceQuery.Where(s => s.RN == 1)
.Insert(db.DestinationTable,
e => new DestinationTable
{
Property1 = 'Constant Value',
Property2 = e.Source.Column1,
Property3 = e.Source.Column2,
Property4 = e.Count
});
After some investigation I stumbled onto this issue. Which lead me to the solution. The code above needs to change to:
db.Execute("TRUNCATE TABLE DESTINATION_TABLE");
db.SourceTable.
Where(s => s.Year > 2012).
GroupBy(s => new { s.Column1, s.Column2 }).
Select(g => new DestinationTable {
Property1 = 'Constant Value',
Property2 = g.First().Column1,
Property3 = g.First().Column2,
Property4 = g.Count(s => s.Column3 == 'Y')
}).Insert(db.DestinationTable, e => e);
Documentation of the linq2db project leaves a bit to be desired however, in terms of functionality its looking like a great project for ETLs (without horrible 1000s of line copy/paste sql/ssis scripts).

Convert a SQL query to EntityFramework

How can I convert this query to Entity Framework Query?
select Price,
(
select Cost.Title
from Cost
where Cost.CostID= CostItem.CostID
) as nameCost
from CostItem
where ItemID= 11
var result = dbContext.CostItems
.Where(item => item.ItemId == 11)
.Select(item => new { Price = item.Price, nameCose = item.Cost.Title })
Just make sure you have you relations in entity objects properly set up
Your query can also be written with a LEFT JOIN:
SELECT
ci.Price,
NameCost = c.Title
FROM CostItem ci
LEFT JOIN Cost c
ON c.CostID = ci.CostID
WHERE ci.ItemID = 11
Converting this to Linq using C#, you get:
var t =
from ci in CostItem
join c in Cost on ci.CostID equals c.CostID into lc
from c in lc.DefaultIfEmpty()
where ci.ItemID == 11
select new {
Price = ci.Price,
NameCost = c.Title
};

Invalid Object Name, Temporary Table Entity Framework

I´m trying to optimize some process in my application but I´m stuck with this problem. My application is working so the entity mapping is correct. Simplifying what I´m trying to do is this:
using (var offCtx = new CheckinOfflineEntities())
{
using (var trans = offCtx.Database.BeginTransaction(IsolationLevel.Snapshot))
{
DateTime purgePivot = DateTime.Now.AddDays(-2);
count = offCtx.Database.ExecuteSqlCommand(#"select L.* into #NewLegs from InventoryLeg L where L.STDUTC >= {0}", purgePivot);
long d = offCtx.Database.SqlQuery<long>("select count(*) from #NewLegs").FirstOrDefault();
}
}
I´m selecting some data I want to delete from one table, storing it in a temporary table so that I can use this temporary table in other queries to exclude related data.
The problem is, when I try to use the temporary table I´m receiving the exception SqlException: "Invalid object name '#NewLegs'."
Thank you for your time.
You can merge the query like this.
And count returns int, not long.
COUNT always returns an int data type value. - MSDN
var query = string.Format("{0} {1}",
#"select L.* into #NewLegs from InventoryLeg L where L.STDUTC >= #STDUTC",
#"select count(*) from #NewLegs")
var d = offCtx.Database.SqlQuery<int>(query, new SqlParameter("STDUTC", purgePivot))
.FirstOrDefault();
I realy don´t know why but removing the parameter and adding it in the query solved the problem.
The code below works fine:
using (var offCtx = new CheckinOfflineEntities())
{
using (var trans = offCtx.Database.BeginTransaction(IsolationLevel.Snapshot))
{
DateTime purgePivot = DateTime.Now.AddDays(-2);
count = offCtx.Database.ExecuteSqlCommand(#"select L.* into #NewLegs from InventoryLeg L where L.STDUTC >= " + purgePivot.toString("yyyy-mm-dd HH:mm:ss");
long d = offCtx.Database.SqlQuery<long>("select count(*) from #NewLegs").FirstOrDefault();
}
}

How to use IN Clause for list of strings or GUID's in Dapper

I am trying to write a dapper query for IN clause, but it's not working throwing casting error saying "Conversion failed when converting the nvarchar value 'A8B08B50-2930-42DC-9DAA-776AC7810A0A' to data type int." . In below query fleetAsset is Guid converted into string.
public IQueryable<MarketTransaction> GetMarketTransactions(int fleetId, int userId, int rowCount)
{
//Original EF queries which I am trying to convert to Dapper
//var fleetAsset = (from logicalFleetNode in _context.LogicalFleetNodes
// where logicalFleetNode.LogicalFleetId == fleetId
// select logicalFleetNode.AssetID).ToList();
////This query fetches guid of assetprofiles for which user having permissions based on the assets user looking onto fleet
//var assetProfileIds = (from ap in _context.AssetProfileJoinAccounts
// where fleetAsset.Contains(ap.AssetProfile.AssetID) && ap.AccountId == userId
// select ap.AssetProfileId).ToList();
var fleetAsset = _context.Database.Connection.Query<string>("SELECT CONVERT(varchar(36),AssetID) from LogicalFleetNodes Where LogicalFleetId=#Fleetid",
new { fleetId }).AsEnumerable();
//This query fetches guid of assetprofiles for which user having permissions based on the assets user looking onto fleet
var sql = String.Format("SELECT TOP(#RowCount) AssetProfileId FROM [AssetProfileJoinAccounts] AS APJA WHERE ( EXISTS (SELECT " +
"1 AS [C1] FROM [dbo].[LogicalFleetNodes] AS LFN " +
"INNER JOIN [dbo].[AssetProfile] AS AP ON [LFN].[AssetID] = [AP].[AssetID]" +
" WHERE ([APJA].[AssetProfileId] = [AP].[ID]) " +
" AND ([APJA].[AccountId] = #AccountId AND LogicalFleetId IN #FleetId)))");
var assetProfileIds = _context.Database.Connection.Query<Guid>(sql, new { AccountId = userId, FleetId = fleetAsset, RowCount=rowCount });
Dapper performs expansion, so if the data types match, you should just need to do:
LogicalFleetId IN #FleetId
(note no parentheses)
Passing in a FleetId (typically via an anonymous type like in the question) that is an obvious array or list or similar.
If it isn't working when you remove the parentheses, then there are two questions to ask:
what is the column type of LocalFleetId?
what is the declared type of the local variable fleetAsset (that you are passing in as FleetId)?
Update: test case showing it working fine:
public void GuidIn_SO_24177902()
{
// invent and populate
Guid a = Guid.NewGuid(), b = Guid.NewGuid(),
c = Guid.NewGuid(), d = Guid.NewGuid();
connection.Execute("create table #foo (i int, g uniqueidentifier)");
connection.Execute("insert #foo(i,g) values(#i,#g)",
new[] { new { i = 1, g = a }, new { i = 2, g = b },
new { i = 3, g = c },new { i = 4, g = d }});
// check that rows 2&3 yield guids b&c
var guids = connection.Query<Guid>("select g from #foo where i in (2,3)")
.ToArray();
guids.Length.Equals(2);
guids.Contains(a).Equals(false);
guids.Contains(b).Equals(true);
guids.Contains(c).Equals(true);
guids.Contains(d).Equals(false);
// in query on the guids
var rows = connection.Query(
"select * from #foo where g in #guids order by i", new { guids })
.Select(row => new { i = (int)row.i, g = (Guid)row.g }).ToArray();
rows.Length.Equals(2);
rows[0].i.Equals(2);
rows[0].g.Equals(b);
rows[1].i.Equals(3);
rows[1].g.Equals(c);
}

SQL Server query - combined queries

Does anyone know if it's possible to do the below in one query instead of doing a numrows?
$select1 = "SELECT service FROM UPSServices WHERE code = '$serviceCode' AND ship_from_code = '$shipFrom'";
$result = mssql_query($select1);
//print_r($result);
if(mssql_num_rows($result) == 0) {
$select2 = "SELECT service FROM UPSServices WHERE code = '$serviceCode' AND ship_from_code IS NULL";
$result = mssql_query($select2);
}
while ($service = mssql_fetch_array($result)) {
return $service['0'];
}
You are using SQL Server, so you can do this. It is slightly more complicated than you might expect. The following version counts the number of valid values. If this is greater than 0, then the NULLs are filtered out.
select service
from (SELECT service,
count(ship_from_code) over () as NumValues
FROM UPSServices
WHERE code = '$serviceCode' AND (ship_from_code = '$shipFrom' OR ship_from_code IS NULL)
) t
where (NumValues > 0 and service is not NULL) or (NumValues = 0 and service is NULL)
limit 1
I think you need value with NULL shipCode if specified shipCode does not exists
SELECT service FROM UPSServices WHERE
code = '$serviceCode' AND (ship_from_code = '$shipFrom' OR ship_from_code IS NULL)
ORDER BY ship_from_code"
Order by will make sure that if your ship code exists then its at top

Resources