Given the data below, I am trying to write a LINQ statement that will check if SubscriptionId is null by using (grp2.Select(y => y.SubscriptionId) == null). This must be failing because it will always go into the else section and return ,4 which is not what I am looking for. Is this the correct way to check if this value is null?
LINQPad Example
class Subscription
{
public int? SubscriptionId { get; set; }
public int ParentProductId { get; set; }
public string ParentProductName { get; set; }
public string ChildProductName { get; set; }
public int ChildProductId { get; set; }
public int GroupId { get; set; }
public DateTime? EndDate { get; set; }
}
class SubscriptionViewModel
{
public int SubscriptionId { get; set; }
public int ParentProductId { get; set; }
public string ParentProductName { get; set; }
public string SubscriptionIds { get; set; }
public int GroupId { get; set; }
}
class SubscriptionChildViewModel
{
public string ChildProductName { get; set; }
public int ChildProductId { get; set; }
}
void Main()
{
List<Subscription> ListOfSubscription = new List<Subscription>();
ListOfSubscription.Add(new Subscription() { SubscriptionId = null, ParentProductId = 4, ChildProductId = 4, ParentProductName = "Product 1", ChildProductName = "Product 1", GroupId = 362, });
ListOfSubscription.Add(new Subscription() { SubscriptionId = 2, ParentProductId = 114, ChildProductId = 1, ParentProductName = "Product 2", ChildProductName = "Product 3", GroupId = 1, });
ListOfSubscription.Add(new Subscription() { SubscriptionId = 3, ParentProductId = 114, ChildProductId = 2, ParentProductName = "Product 2", ChildProductName = "Product 4", GroupId = 1, });
var groupedSubscriptions = ListOfSubscription.GroupBy(u => u.GroupId);
var result = groupedSubscriptions.Select(grp1 => new
{
GroupId = grp1.Key,
Subscriptions = grp1.GroupBy(subscr => new
{
subscr.ParentProductId,
subscr.ParentProductName,
})
.Select(grp2 => new SubscriptionViewModel
{
GroupId = grp1.Key,
ParentProductId = grp2.Key.ParentProductId,
ParentProductName = grp2.Key.ParentProductName,
SubscriptionIds = (grp2.Select(y => y.SubscriptionId) == null) ? null : (string.Format("{0},{1}", string.Join(",", grp2.Select(y => y.SubscriptionId)), grp2.Key.ParentProductId))
})
});
var x = result.SelectMany((s => s.Subscriptions));
Console.Write(x);
}
Change your logic
(grp2.Select(y => y.SubscriptionId) == null)
to
grp2.Where(y => y.SubscriptionId == null).Count() > 0
Remove that bracket y.SubscriptionId)
This could work for you, to work with nulleables you can use "HasValue" instead of directly comparing with null.
SubscriptionIds = (grp2.Select( y => y.SubscriptionId.HasValue ) == null) ? null : (string.Format("{0},{1}", string.Join(",", grp2.Select(y => y.SubscriptionId.HasValue)), grp2.Key.ParentProductId))
Microsoft suggests using HasValue for nulleables types
Related
C# MongoDB Driver
public class Details
{
[BsonId]
public ObjectId Id { get; set; }
public string GroupID { get; set; }
public Names[] Name{ get; set; }
}
public class Names
{
public string FullName { get; set; }
public int Age { get; set; }
public int Status {get; set;}
}
Input
var req = [{FullName = "ABC",
Age = 15,
Status = 0}
{FullName = "XYZ",
Age = 16,
Status = 0},
{FullName = "QAZ",
Age = 14,
Status = 0}]
MongoDB Query
var updateDefinationValues = new List<UpdateDefinition<Details>>();
List<FilterDefinition<Details>> listDetailsFilter = new List<FilterDefinition<Details>>();
foreach (var il in req.Names)
{
FilterDefinition<Details> detailsFilter = Builders<Details>.Filter.Where(x => x.GroupID == requestId && x.Names.Any(i => i.FullName == il.FullName));
updateDefinationValues.Add(Builders<Details>.Update.Set(x => x.Names.ElementAt(-1).Status, 1));
listDetailsFilter.Add(detailsFilter);
}
FilterDefinition<Details> filter = Builders<Details>.Filter.Or(test);
var combinedUpdate = Builders<Details>.Update.Combine(updateDefinationValues);
var isUpdated = UpdateOne(_db, filter, combinedUpdate);
The above query is working when listDetailsFilter count == 1.
Error: The positional Operation did not find the match needed for this query
And it is not working when listDetailsFilter count > 1.
This model I would like flat into a view model but the Current Output is not correct it is setting all the ChildProductId to the last value. I am creating one List<SubscriptionViewModel> and then creating multiple SubscriptionViewModel and adding to the collection.
List<Subscription> ListOfSubscriptions = new List<Subscription>();
List<SubscriptionChild> SubscriptionChild = new List<SubscriptionChild>();
SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 1, ChildProductName = "Child 1" });
SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 2, ChildProductName = "Child 2" });
ListOfSubscriptions.Add(new Subscription() { SubscriptionId = 1, ParentProductId=1, ParentProductName = "Product 1",ListOfSubscriptionChild= SubscriptionChild });
SubscriptionChild.Clear();
ListOfSubscriptions.Add(new Subscription() { SubscriptionId = 2, ParentProductId = 2, ParentProductName = "Product 2"});
SubscriptionChild.Clear();
SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 3, ChildProductName = "Child 3" });
SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 4, ChildProductName = "Child 4" });
ListOfSubscriptions.Add(new Subscription() { SubscriptionId = 3, ParentProductId = 3, ParentProductName = "Product 3", ListOfSubscriptionChild = SubscriptionChild });
List<SubscriptionViewModel> SubscriptionViewModel = new List<SubscriptionViewModel>();
foreach (var Subscription in ListOfSubscriptions)
{
SubscriptionViewModel vm = new SubscriptionViewModel();
vm.SubscriptionId = Subscription.SubscriptionId;
vm.ParentProductId = Subscription.ParentProductId;
vm.ParentProductName = Subscription.ParentProductName;
int count = Subscription.ListOfSubscriptionChild == null ? 0 : Subscription.ListOfSubscriptionChild.Count;
if (count == 0) {
SubscriptionViewModel.Add(vm);
}
else
{
var listOfChild = Subscription.ListOfSubscriptionChild.ToList();
foreach (var item in listOfChild)
{
vm.ChildProductId = item.ChildProductId;
vm.ChildProductName = item.ChildProductName;
SubscriptionViewModel.Add(vm);
}
}
}
foreach (var item in SubscriptionViewModel)
{
Console.WriteLine(string.Format("SubscriptionId{0} ParentProductId-{1} ChildProductId-{2}", item.SubscriptionId, item.ParentProductId, item.ChildProductId));
}
class Subscription
{
public int SubscriptionId { get; set; }
public int ParentProductId { get; set; }
public string ParentProductName { get; set; }
public List<SubscriptionChild> ListOfSubscriptionChild { get; set; }
}
class SubscriptionChild
{
public string ChildProductName { get; set; }
public int ChildProductId { get; set; }
}
class SubscriptionViewModel
{
public int SubscriptionId { get; set; }
public int ParentProductId { get; set; }
public string ParentProductName { get; set; }
public string ChildProductName { get; set; }
public int ChildProductId { get; set; }
}
Current Output
SubscriptionId1 ParentProductId-1 ChildProductId-4
SubscriptionId1 ParentProductId-1 ChildProductId-4
SubscriptionId2 ParentProductId-2 ChildProductId-0
SubscriptionId3 ParentProductId-3 ChildProductId-4
SubscriptionId3 ParentProductId-3 ChildProductId-4
expected outcome
SubscriptionId1 ParentProductId-1 ChildProductId-1
SubscriptionId1 ParentProductId-1 ChildProductId-2
SubscriptionId2 ParentProductId-2 ChildProductId-0
SubscriptionId3 ParentProductId-3 ChildProductId-3
SubscriptionId3 ParentProductId-3 ChildProductId-4
You're overwriting your SubscriptionViewModel for all of your children. You need to create a new one for each child:
foreach (var Subscription in ListOfSubscriptions)
{
int count = Subscription.ListOfSubscriptionChild == null ? 0 : Subscription.ListOfSubscriptionChild.Count;
if (count == 0)
{
SubscriptionViewModel vm = new SubscriptionViewModel();
vm.SubscriptionId = Subscription.SubscriptionId;
vm.ParentProductId = Subscription.ParentProductId;
vm.ParentProductName = Subscription.ParentProductName;
SubscriptionViewModel.Add(vm);
}
else
{
var listOfChild = Subscription.ListOfSubscriptionChild.ToList();
foreach (var item in listOfChild)
{
// Instantiate a new model for each child
SubscriptionViewModel vm = new SubscriptionViewModel();
vm.SubscriptionId = Subscription.SubscriptionId;
vm.ParentProductId = Subscription.ParentProductId;
vm.ParentProductName = Subscription.ParentProductName;
vm.ChildProductId = item.ChildProductId;
vm.ChildProductName = item.ChildProductName;
SubscriptionViewModel.Add(vm);
}
}
}
This was kind of a hard question to ask but this is my problem:
I'm populating a grid with data I obtain from a different class, this class uses a (generic) Model that can represent multiple models:
Model(can represent Vessel or Container):
public class DataGridInstallationRow
{
[Key]
public string Name { get; set; }
//Vessel
public int IMO { get; set; }
public int MMSI { get; set; }
public int EEOI { get; set; }
public int FOC { get; set; }
[Display(Name = "Total Fuel Mass")]
public int TotalFuelMass { get; set; }
[Display(Name = "Average Speed")]
public int AverageSpeed { get; set; }
[Display(Name = "Total Distance Sailed")]
public int TotalDistanceSailed { get; set; }
//Container
[Display(Name = "Generated by Sun")]
public int EnergyGeneratedBySun { get; set; }
[Display(Name = "Generated by Wind")]
public int EnergyGeneratedByWind { get; set; }
[Display(Name = "Generated by Generator")]
public int EnergyGeneratedByGenerator { get; set; }
[Display(Name = "Consumed by EV's")]
public int EnergyConsumedByEV { get; set; }
[Display(Name = "Consumed by Construction Site")]
public int EnergyConsumedByConstructionSite { get; set; }
}
This model is used in my provider:
if (fleet.Type.Equals("Container"))
{
return Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
{
Name = $"Container {i}",
EnergyGeneratedBySun = 13,
EnergyGeneratedByWind = 19,
EnergyGeneratedByGenerator = 3,
EnergyConsumedByEV = 15,
EnergyConsumedByConstructionSite = 24
}).ToList();
}
else
{
return Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
{
Name = $"Vessel {i}",
IMO = 231,
MMSI = 1344,
EEOI = 8121,
FOC = 123,
TotalFuelMass = 6817,
AverageSpeed = 14,
TotalDistanceSailed = 1560
}).ToList();
}
As u can see, depending on the Fleet.Type, one of the other is filled in. If Fleet.Type is container the object will look like this:
As u can see the properties of "Vessel" is filled in aswell with all "0", I want these to be null instead of "0" because my datagrid is filled with both models now:
Whats best practice to avoid and fix this?
UPDATE
Applied solution of Dogac:
if (fleet.Type.Equals("Container"))
{
return Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
{
Name = $"Container {i}",
EnergyGeneratedBySun = 13,
EnergyGeneratedByWind = 19,
EnergyGeneratedByGenerator = 3,
EnergyConsumedByEV = 15,
EnergyConsumedByConstructionSite = 24
}).Where(row =>
{
return row.EnergyGeneratedBySun.HasValue &&
row.EnergyGeneratedByWind.HasValue &&
row.EnergyGeneratedByGenerator.HasValue &&
row.EnergyConsumedByEV.HasValue &&
row.EnergyConsumedByConstructionSite.HasValue;
}).ToList();
}
else
{
return Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
{
Name = $"Vessel {i}",
IMO = 231,
MMSI = 1344,
EEOI = 8121,
FOC = 123,
TotalFuelMass = 6817,
AverageSpeed = 14,
TotalDistanceSailed = 1560
}).Where(row =>
{
return row.IMO.HasValue &&
row.MMSI.HasValue &&
row.EEOI.HasValue &&
row.FOC.HasValue &&
row.TotalFuelMass.HasValue &&
row.AverageSpeed.HasValue &&
row.TotalDistanceSailed.HasValue;
}).ToList();
}
Is still not working, im again receiving a list with nullable items.
Thanks in advance
Try making all properties nullable.
Like this:
// Vessel
public int? IMO { get; set; }
public int? MMSI { get; set; }
public int? EEOI { get; set; }
public int? FOC { get; set; }
Edit regarding comment:
Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
{
Name = $"Vessel {i}",
IMO = 231,
MMSI = 1344,
EEOI = 8121,
FOC = 123,
TotalFuelMass = 6817,
AverageSpeed = 14,
TotalDistanceSailed = 1560
}).Where(row =>
{
return row.IMO.HasValue &&
row.MMSI.HasValue &&
row.EEOI.HasValue &&
row.FOC.HasValue &&
row.TotalFuelMass.HasValue &&
row.AverageSpeed.HasValue &&
row.TotalDistanceSailed.HasValue;
}).ToList();
I'm getting the results of a sql outer join as flat results in an IEnumerable, and would like to convert them to nested typed objects in linq. From something like this:
[{id: 1, industryId: 1}, {id:1, industryId: 2}, {id:2, industryId: 1} etc..]
to something like this:
list of Company [{id: 1, list of Industry{industryId: 1, 2}, {id: 2, list of Industry{industryId: 1}}]
I'm currently trying a solution with GroupBy:
Companies = flatDbRows
.GroupBy(
row => row.CompanyId,
(key, value) => new CompanyModel
{
CompanyId = value.First().CompanyId,
CompanyName = value.First().CompanyName,
Industries = value
.GroupBy(
row => new { row.IndustryId, row.Industry },
(k, v) => new IndustryModel() { IndustryId = k.IndustryId, Name = k.Industry }
)
.Where(x => x.IndustryId != 0)
.ToList(),
}).ToList();
}
but it doesn't feel great, especially with all the value.First() I'm using to get the values that only belong to each grouped company. Is there something more appropriate? Group join sounded more like what I wanted, but I'm having trouble understanding how to apply it to a single list. I'm open to using query syntax instead of the lambdas if that's easier.
I'm trying to go from this model (where company-related info will be duplicated for each outer joined industry result):
public class CompanyFlatDbRowsModel
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public int IndustryId{ get; set; }
public string Industry { get; set; }
}
to this:
public class CompanyModel
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public IEnumerable<IndustryModel> Industries { get; set; }
}
// FULL edit after providing your models
public class TestClass
{
public class CompanyModel
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public List<IndustryModel> Industires { get; set; }
}
public class IndustryModel
{
public int IndustryId { get; set; }
public string IndustryName { get; set; }
}
public class CompanyFlatDbRowsModel
{
public CompanyFlatDbRowsModel()
{
}
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public int IndustryId { get; set; }
public string Industry { get; set; }
}
[Fact]
public void Test()
{
var data = new List<CompanyFlatDbRowsModel>
{
new CompanyFlatDbRowsModel
{
CompanyId = 1,
CompanyName = "Company 1",
IndustryId = 1,
Industry = "Industry 1"
},
new CompanyFlatDbRowsModel
{
CompanyId = 1,
CompanyName = "Company 1",
IndustryId = 2,
Industry = "Industry 2"
},
new CompanyFlatDbRowsModel
{
CompanyId = 2,
CompanyName = "Company 2",
IndustryId = 3,
Industry = "Industry 3"
},
new CompanyFlatDbRowsModel
{
CompanyId = 2,
CompanyName = "Company 2",
IndustryId = 4,
Industry = "Industry 4"
},
};
var result = data.GroupBy(x => x.CompanyId)
.Select(x => new CompanyModel()
{
CompanyId = x.Key,
CompanyName = x.First().CompanyName,
Industires = x.Select(y=> new IndustryModel
{
IndustryName = y.Industry,
IndustryId = y.IndustryId
}).ToList()
}).ToList();
foreach (var item in result)
{
var text = $"Company id : {item.CompanyId}, industries : {string.Join(',',item.Industires.Select(x=>$"(name: {x.IndustryName}, id: {x.IndustryId})"))}";
Debug.WriteLine(text);
}
}
}
output:
Company id : 1, industries : (name: Industry 1, id: 1),(name: Industry 2, id: 2)
Company id : 2, industries : (name: Industry 3, id: 3),(name: Industry 4, id: 4)
edit:
alternatively you can do as below, however the "first" thing still occurs somewhere, I have tried also the GroupJoin but it doesn't really help in that case.
var otherResult = data.Select(x =>
new CompanyModel
{
CompanyId = x.CompanyId,
CompanyName = x.CompanyName,
Industires = data
.Where(y => y.CompanyId == x.CompanyId)
.Select(y => new IndustryModel
{
IndustryId = y.IndustryId,
IndustryName = y.Industry
}).ToList()
})
.GroupBy(y => y.CompanyId)
.Select(x => x.First())
.ToList();
edit:
one more approach without using "first"
var anotherResult = data.GroupBy(x => x.CompanyId)
.Select(x =>
{
var companyModel = new CompanyModel()
{
CompanyId = x.Key
};
companyModel.Industires = x.Select(y =>
{
companyModel.CompanyName = y.CompanyName; // assignign here occurs multiple times however with the same value
return new IndustryModel
{
IndustryId = y.IndustryId,
IndustryName = y.Industry
};
}).ToList();
return companyModel;
}).ToList();
In database I have two tables:
public partial class PersonOne
{
public int id { get; set; }
public string name { get; set; }
public string surname { get; set; }
}
public partial class PersonTwo
{
public int id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
}
I would like to fill my set:
public class PersonOnePersonTwo
{
public int PersonOneId { get; set; }
public int PersonTwoId { get; set; }
}
where PersonOne.name == PersonTwo.firstname && PersonOne.surname == PersonTwo.lastname but I have no idea how I can do that - because below code isn't efficient and is so slow:
List<PersonOne> personOneList = new List<PersonOne>();
List<PersonTwo> personTwoList = new List<PersonTwo>();
List<PersonOnePersonTwo> personOnePersonTwoList = new List<PersonOnePersonTwo>();
foreach (PersonOne personOne in personOneList)
{
foreach(PersonTwo personTwo in personTwoList.Where(x => x.firstname == personOne.name && x.lastname == personOne.surname).ToList())
{
personOnePersonTwoList.Add(new PersonOnePersonTwo
{
PersonOneId = personOne.id,
PersonTwoId = personTwo.id
});
}
};
Try this:
var result = personOneList.Join
(
personTwoList,
person1 => new { Key1 = person1.Name, Key2 = person1.Surname },
person2 => new { Key1 = person2.FirstName, Key2 = person2.LastName },
(person1, person2) => new PersonOnePersonTwo { PersonOneId = person1.Id, PersonTwoId = person2.Id }
).ToList();
I would go with:
var personOnePersonTwoList = new List<PersonOnePersonTwo>();
foreach (var personOne in personOneList)
{
personOnePersonTwoList = personTwoList.Where(x => x.firstname.Equals(personOne.name, StringComparison.OrdinalIgnoreCase) &&
x.lastname.Equals(personOne.surname, StringComparison.OrdinalIgnoreCase))
.Select(x => new PersonOnePersonTwo {PersonOneId = personOne.id, PersonTwoId = x.id}).ToList();
};
As a side note: it's more convinient to use Equals when comparing strings.