Hi
I have applied Left join in Linq and return type of my function is Object. Now I am binding this object to Repeater. Works perfectly. But I want to perform other action when there is no value in the object. How we can check?
var result = getTemporary();
if(result!=null && result.Count>0) //!=null not working and .Count is not in the property list. I have tried result.ToString().Count() but no luck.
{
//Do binding……
}
public static Object getTemporaryMaterialIn(string sessionKey)
{
InventoryEntities context = new InventoryEntities();
var aa = (from tm in context.MaterialInwardTemporaries
join p in context.Products
on tm.MatInTemp_Product equals p.productId into prdct //=-=-=-= Join with products table.
from prdct2 in prdct.DefaultIfEmpty()
join pcom in context.ProductCompanies
on tm.MatInTemp_ProductCompany equals pcom.prdctCompId into prdctComp //=-=-=-= Join with product Company Table.
from prdctComp2 in prdctComp.DefaultIfEmpty()
orderby tm.MatInTemp_CreatedOn
where tm.MatInTemp_SessionKey == sessionKey
select new
{
prdctComp2.prdctCompName,
prdct2.productName,
tm.MatInTemp_Quantity,
tm.MatInTemp_DealerPrice,
tm.MatInTemp_MRP,
tm.MatInTemp_Vat,
tm.MatInTemp_TotalAmount,
tm.MatInTemp_Id
}).ToList();
return aa;
}
On code behind
var a=ClassMaterialInwardTemp.getTemporaryMaterialIn("key"); //=-= I want to check when a is null or having no result.
Since you are returning a List, Maybe you can convert to ICollection and have a check:
publicpartialclassWebForm1 : System.Web.UI.Page
{
publicstaticObject getTemporaryMaterialIn(string sessionKey)
{
InventoryEntities context = new InventoryEntities();
var aa = (from tm in context.MaterialInwardTemporaries
join p in context.Products
on tm.MatInTemp_Product equals p.productId into prdct //=-=-=-= Join with products table. from prdct2 in prdct.DefaultIfEmpty()
join pcom in context.ProductCompanies
on tm.MatInTemp_ProductCompany equals pcom.prdctCompId into prdctComp //=-=-=-= Join with product Company Table. from prdctComp2 in prdctComp.DefaultIfEmpty()
orderby tm.MatInTemp_CreatedOn
where tm.MatInTemp_SessionKey == sessionKey
selectnew
{
prdctComp2.prdctCompName,
prdct2.productName,
tm.MatInTemp_Quantity,
tm.MatInTemp_DealerPrice,
tm.MatInTemp_MRP,
tm.MatInTemp_Vat,
tm.MatInTemp_TotalAmount,
tm.MatInTemp_Id
}).ToList();
return aa;
}
protectedvoid Page_Load(object sender, EventArgs e)
{
var result = getTemporaryMaterialIn("1") asICollection;
if (result != null && result.Count > 0)
{
}
}
}
anuj_koundal
Contributor
2088 Points
495 Posts
How to check for NULL object in LINQ Join.
Nov 21, 2012 09:54 AM|LINK
Hi
I have applied Left join in Linq and return type of my function is Object. Now I am binding this object to Repeater. Works perfectly. But I want to perform other action when there is no value in the object. How we can check?
This is how my function looks :
public static Object getTemporary() { }Regards
Anuj Koundal
alankarp
Contributor
2042 Points
345 Posts
Re: How to check for NULL object in LINQ Join.
Nov 21, 2012 10:34 AM|LINK
Hi,
you can use repeater.items.count properly. it will be zero if your object is null
Thanks
Alankar
Profile
anuj_koundal
Contributor
2088 Points
495 Posts
Re: How to check for NULL object in LINQ Join.
Nov 21, 2012 10:41 AM|LINK
I know it but I want to check before
Repeater.Datasourse code
Regards
Anuj Koundal
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to check for NULL object in LINQ Join.
Nov 22, 2012 12:19 AM|LINK
Hello,
Just check getTemporary is null or Empty, please use this:
var result = getTemporary(); if(result!=null && result.Count>0) { //Do binding…… }anuj_koundal
Contributor
2088 Points
495 Posts
Re: How to check for NULL object in LINQ Join.
Nov 22, 2012 03:07 AM|LINK
Not Working Mate
Regards
Anuj Koundal
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to check for NULL object in LINQ Join.
Nov 22, 2012 03:33 AM|LINK
Any exceptions? Errors?
anuj_koundal
Contributor
2088 Points
495 Posts
Re: How to check for NULL object in LINQ Join.
Nov 22, 2012 03:46 AM|LINK
No exceptions no errors
var result = getTemporary(); if(result!=null && result.Count>0) //!=null not working and .Count is not in the property list. I have tried result.ToString().Count() but no luck. { //Do binding…… }Regards
Anuj Koundal
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to check for NULL object in LINQ Join.
Nov 22, 2012 03:49 AM|LINK
Please make sure that your getTemporary() return an instance that has implemented IEnumerable.
anuj_koundal
Contributor
2088 Points
495 Posts
Re: How to check for NULL object in LINQ Join.
Nov 22, 2012 04:08 AM|LINK
Here is my complete code
public static Object getTemporaryMaterialIn(string sessionKey) { InventoryEntities context = new InventoryEntities(); var aa = (from tm in context.MaterialInwardTemporaries join p in context.Products on tm.MatInTemp_Product equals p.productId into prdct //=-=-=-= Join with products table. from prdct2 in prdct.DefaultIfEmpty() join pcom in context.ProductCompanies on tm.MatInTemp_ProductCompany equals pcom.prdctCompId into prdctComp //=-=-=-= Join with product Company Table. from prdctComp2 in prdctComp.DefaultIfEmpty() orderby tm.MatInTemp_CreatedOn where tm.MatInTemp_SessionKey == sessionKey select new { prdctComp2.prdctCompName, prdct2.productName, tm.MatInTemp_Quantity, tm.MatInTemp_DealerPrice, tm.MatInTemp_MRP, tm.MatInTemp_Vat, tm.MatInTemp_TotalAmount, tm.MatInTemp_Id }).ToList(); return aa; }On code behind
var a=ClassMaterialInwardTemp.getTemporaryMaterialIn("key"); //=-= I want to check when a is null or having no result.
Regards
Anuj Koundal
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to check for NULL object in LINQ Join.
Nov 22, 2012 04:15 AM|LINK
Hello again,
Since you are returning a List, Maybe you can convert to ICollection and have a check: