Non-Static Method Requires a Target

Non-static method requires a target

I think this confusing exception occurs when you use a variable in a lambda which is a null-reference at run-time. In your case, I would check if your variable calculationViewModel is a null-reference.

Something like:

public ActionResult MNPurchase()
{
CalculationViewModel calculationViewModel = (CalculationViewModel)TempData["calculationViewModel"];

if (calculationViewModel != null)
{
decimal OP = landTitleUnitOfWork.Sales.Find()
.Where(x => x.Min >= calculationViewModel.SalesPrice)
.FirstOrDefault()
.OP;

decimal MP = landTitleUnitOfWork.Sales.Find()
.Where(x => x.Min >= calculationViewModel.MortgageAmount)
.FirstOrDefault()
.MP;

calculationViewModel.LoanAmount = (OP + 100) - MP;
calculationViewModel.LendersTitleInsurance = (calculationViewModel.LoanAmount + 850);

return View(calculationViewModel);
}
else
{
// Do something else...
}
}

C# Query - Non Static Method Requires a Target

The FirstOrDefault method may return null value if no query results returned there:

var orderItem = _context.PurchaseOrderItems.FirstOrDefault(
p => p.JobReference == item.JobReference && p.ItemNumber == item.ItemNumber);

Since orderItem.Id throws NullReferenceException when orderItem is null, it will propagate to LINQ throwing TargetException as mentioned in question (see this post and this post for more info).

Hence, you need to check presence of null value from orderItem by modifying second LINQ query to this one:

return _context.DeliverySchedules.Include(d => d.PurchaseOrderItem)
.Where(d => (orderItem != null && d.PurchaseOrderItem.Id == orderItem.Id))
.ToList();

NB: Null checking must takes place before retrieving property Id of orderItem to prevent NullReferenceException.

As an alternative, if condition to check against null value may be used without modifying second query:

if (orderItem != null)
{
return _context.DeliverySchedules.Include(d => d.PurchaseOrderItem)
.Where(d => d.PurchaseOrderItem.Id == orderItem.Id)
.ToList();
}

Non static method requires a target

You should first test if currUser is null or not and your dbContext too.

if (currUser == null) return false;
if (dbContext == null) throw new Exception ("The dbContext has not been set");

Secondly, you can simplify your query like yhat :

 var validateUser = dbContext.PMS_USERS.Where(p=> p.LOGICALREF != currUser.LOGICALREF &&  p.USERNAME == currUser.USERNAME);

And then change the return statement to :

return (validateUser.FirstOrDefault() != null);

You can alternativelly use SingleOrDefault statement insead of FirstOrDefault, if you want to be sure there is only one user corresponding to your criteria.

What does System.Reflection.TargetException: Non-static method requires a target. mean?

It means non static method requires an object. If you have an instance member then you have to use an instance to get it's value. Because without an instance it doesn't exist.So you need to pass an instance of the type instead of null to GetValue method.Or make the member static if you don't want it to be an instance member.

Non static method requires a target C# Entity Framework

You can try to move the res != null decision out of the query.

public InventorySales  getinvSales(int mgrId, int SalesId) {
var res = from ins in sbdb.tblManager
where(managerId == mgrID)
select new Managerset() {
Manager = ins.Manager
}).firstOrDefault();

InventorySales invSales = (from x in sbdb.tblSalesn.where(i=>i.salesId == Sid)
join y in sbdb.tblProds on x.salesid equals y.salesid into resSales
from xyz in result.DefaultIfEmpty()
select new InventorySales()
{
ProductName = x.productname,
Location = (xyz!= null) ? xyz.location:string.empty,
Manager = string.empty
}).FirstOrDefault();

if (invSales != null && res != null) {
invSales.Manager = res.Manager;
}

return invSales
}

Linq where condition with nullable object property result in invoke non static method requires a target

So, if devTab is null you want a new empty list of anonymous type.. Awkward, but doable:

DeviceTable devTab = (from t in _db.DeviceTables
where t.DeviceType == devtype && t.IDPlant == id
select t)
.FirstOrDefault();

var DeviceTabColumnsNameAndDesc = devTab == null ?
Enumerable.Empty<object>().Select(x => new { colName = "", colDescr = "" }).ToList() :
(
from t in _db.DeviceTabCols
where t.IDDevTab == devTab.ID && t.MeasureFamily != "KEY" && t.MeasureFamily != "DATETIME"
select new {
colName = t.ColumnName,
colDescr = t.ColumnDescr
}
).ToList();

If you wanted to switch away from anonymous types in this case, you could look at ValueTuple:

var DeviceTabColumnsNameAndDesc = devTab == null ?
new List<(string colName, string colDesc)>() :
(
from t in _db.DeviceTabCols
where t.IDDevTab == devTab.ID && t.MeasureFamily != "KEY" && t.MeasureFamily != "DATETIME"
select (
colName: t.ColumnName,
colDescr: t.ColumnDescr
)
).ToList();

Or make a record, which is like a class but with some extra bits that make it useful as a data holder:

//defined in a namespace
public record ColThing(string ColName, string ColDesc);

var DeviceTabColumnsNameAndDesc = devTab == null ?
new List<ColThing>() :
(
from t in _db.DeviceTabCols
where t.IDDevTab == devTab.ID && t.MeasureFamily != "KEY" && t.MeasureFamily != "DATETIME"
select new ColThing(t.ColumnName, t.ColumnDescr)
).ToList();


Related Topics



Leave a reply



Submit