Dynamic Linq Library can’t handling duplicate alias column names

dynamic-linq dynamic-queries expression-trees linq linq-expressions

Question

I am trying to collect the data from database by using Dynamic Linq Library NeGet. When I loop through it showing this error ‘The item with identity 'FirstName' already exists in the metadata collection’

Seems like Dynamic Linq Library NuGet is can’t handle duplicate alias column names.

I have two tables and it has One-On-Many relationship as follow, containing the same Columns names in Lead table and CoBorrower table.

Table1: Lead

Columns: LeadID, FirstName,DateCreated

Table2: CoBorrower

Columns: LeadID, CoBorrowerID,FirstName,Tax,DateCreated

Here is my code snippet

var res = (from l in db.Leads
join db.CoBorrower.GetAll()
on l.LeadID equals cb.LeadID
select new { l, cb }).AsQueryable();
string myCustomCondition="FistName=myname";
IQueryable iq = res.Where(myCustomCondition)
                .OrderBy(reportBulder.Group1)
                .Select("new(l.LeadID,l.FirstName,cb.FistName)")
                .GroupBy("LeadID", "it")
                .Select("new (it.Key as Key, it as Value)");
foreach (dynamic group in iq)
{
 string Key = group.Key.ToString();
 List<dynamic> items = new List<dynamic>();
  foreach (dynamic album in group.Value)
   {
    items.Add(album);
   }
 dataList.Add(Key, items);
}

enter image description here

I will appreciate your help in advance.

1
1
4/20/2015 3:37:31 PM

Accepted Answer

This is logical. The .Select("new(l.LeadID,l.FirstName,cb.FistName)") will create an anonymous object like:

new 
{
    LeadID = ....,
    FirstName = ....,
    FirstName = ....,
}

that is illegal (two properties with the same name)

Use the as

.Select("new(l.LeadID,l.FirstName,cb.FistName as FirstName2)")

so that the anonymous object created is

new 
{
    LeadID = ....,
    FirstName = ....,
    FirstName2 = ....,
}

as you do in the second .Select.

1
4/22/2015 6:44:01 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow