C# LINQ left join unmatch data will not be populated

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
see my code

//creating Product DataTable
DataTable dt = new DataTable();
DataRow dr = null;
dt.TableName = "Product";
dt.Columns.Add("Id", typeof(int));
dt.Columns[0].AutoIncrementSeed = 1;
dt.Columns[0].AutoIncrement = true;
dt.Columns.Add("Product Name");
dt.Columns.Add("Brand Name");
dt.Columns.Add("Tax Id", typeof(int));

dr = dt.NewRow();
dr["Product Name"] = "Laptop";
dr["Brand Name"] = "Samsung";
dr["Tax Id"] = 1;
dt.Rows.Add(dr);

DataRow dr1 = null;
dr1 = dt.NewRow();
dr1["Product Name"] = "Mouse";
dr1["Brand Name"] = "Dell";
dr1["Tax Id"] = 1;
dt.Rows.Add(dr1);


DataRow dr2 = null;
dr2 = dt.NewRow();
dr2["Product Name"] = "Mobile";
dr2["Brand Name"] = "Apple";
dr2["Tax Id"] = 1;
dt.Rows.Add(dr2);

DataRow dr3 = null;
dr3 = dt.NewRow();
dr3["Product Name"] = "Book";
dr3["Brand Name"] = "C# Corner Press";
dr3["Tax Id"] = 2;
dt.Rows.Add(dr3);

DataRow dr4 = null;
dr4 = dt.NewRow();
dr4["Product Name"] = "Toys";
dr4["Brand Name"] = "Test";
dr4["Tax Id"] = 5;
dt.Rows.Add(dr4);

//creating Tax DataTable
DataTable dtTax = new DataTable();

dtTax.TableName = "taxmaster";
dtTax.Columns.Add("Tax Id", typeof(int));
dtTax.Columns[0].AutoIncrementSeed = 1;
dtTax.Columns[0].AutoIncrement = true;
dtTax.Columns.Add("Product Category");
dtTax.Columns.Add("Charge", typeof(int));
DataRow drtax = null;

drtax = dtTax.NewRow();
drtax["Product Category"] = "Electronics";
drtax["Charge"] = 10;
dtTax.Rows.Add(drtax);

DataRow drtax1 = null;
drtax1 = dtTax.NewRow();
drtax1["Product Category"] = "Educational";
drtax1["Charge"] = 8;
dtTax.Rows.Add(drtax1);


var JoinResult2 = (from p in dt.AsEnumerable()
join t in dtTax.AsEnumerable()
on p.Field<int>("Tax Id") equals t.Field<int>("Tax Id") into pj
from r in pj.DefaultIfEmpty()
select new
{
ProductName = p.Field<string>("Product Name"),
BrandName = p.Field<string>("Brand Name"),
ProductCategory =r == null ? "" : r.Field<string>("Product Category"),
TaxCharge = r == null ? 0 : r.Field<int>("Charge")


}).ToList().ToDataTable();



public static class Extension
{
public static DataTable ToDataTable<T>(this List<T> iList)
{
DataTable dataTable = new DataTable();
PropertyDescriptorCollection propertyDescriptorCollection =
TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
{
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection;
Type type = propertyDescriptor.PropertyType;

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = Nullable.GetUnderlyingType(type);


dataTable.Columns.Add(propertyDescriptor.Name, type);
}
object[] values = new object[propertyDescriptorCollection.Count];
foreach (T iListItem in iList)
{
for (int i = 0; i < values.Length; i++)
{
values = propertyDescriptorCollection.GetValue(iListItem);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
}


When r will be null then i want there should be no data in my list which i want accomplish in LINQ select new.....how to do it ?

Continue reading...
 
Back
Top