How To Remove List From Another List In C# - c#

Here I have two list var userListFromFriendTable = mainService.GetUserFromFriend(ChattingUserName); and var getAllUserFromMessageUserTable = mainService.GetalluserfromMessageUser(); whose implementation are
given below.
Now what is want is to remove all the Friend table FriendRequestReceiverName column data of userListFromFriendTable list from MessageUser table MessageUserName column of getAllUserFromMessageUserTable list.
Any help will be grate
public List<Friend> GetUserFromFriend(string chattingUserName)
{
var checkUser = uow.Repository<Friend>().FindBy(x => x.FriendRequestSenderName.ToLower() == chattingUserName.ToLower()).ToList();
return checkUser;
}
public List<MessageUser> GetalluserfromMessageUser()
{
var checkUser = uow.Repository<MessageUser>().GetAll().ToList();
return checkUser;
}
public class Friend
{
public int FriendId { get; set; }
public string FriendRequestSenderName { get; set; }
public string FriendRequestReceiverName { get; set; }
public int IsConfirmed { get; set; }
}
public class MessageUser
{
public int Id { get; set; }
public string MessageUserName { get; set; }
public int IsFriend { get; set; }
}

try this:
var names=userListFromFriendTable.Select(i=> i.FriendRequestReceiverName)
.OfType<string>()
.ToArray();
getAllUserFromMessageUserTable.RemoveAll(r => names.Contains( r.MessageUserName );

Related

How to insert into an enumerable list, which is an member of another list

I have an AcadamicFee list, at one point in time I want to insert an item under Fee. So that, it should add like Fee[1], Fee[2].modified the code like below.
I want to insert one new item under Fee list. But it didn't reflect. How to update the AcadamicFee list through Linq?
Thanks in advance.
AcadamicFee[0]
{
----
----
----
TermDetails [0]
{
---
Fee[0]
Fee[1]
Fee[2]
}
}
public class Fee
{
public int FeeID { get; set; }
public string FeeName { get; set; }
public string FeeAmount { get; set; }
public string IsFineApplicable { get; set; }
public string LastDate { get; set; }
}
public class Ter
{
public int TermID { get; set; }
public string TermName { get; set; }
public IEnumerable<Fee> FeeDetails { get; set; }
}
public class AcadamicFee
{
public int id { get; set; }
public string AdmissionID { get; set; }
public string StudentName { get; set; }
public int Class { get; set; }
public string AcadamicYear { get; set; }
public Nullable<int> TotalPaid { get; set; }
public Nullable<int> AcadamicAmount { get; set; }
public Nullable<int> BalanceAmount { get; set; }
public IEnumerable<Term> TermDetails { get; set; }
}
Any clue how to do with Linq? I tried something like this:
for (var i = 0; i < fees.Count; i++)
{
bool termDetailsExist = acadamic_fee.Any(a => a.TermDetails.Any(b => b.TermID == fees[i].TermID));
if (termDetailsExist)
{
foreach (AcadamicFeeModel acm in acadamic_fee)
{
foreach (TermModel trm in acm.TermDetails)
{
if (trm.TermID == fees[i].TermID)
{
List<FeeModel> FeeType = new List<FeeModel>();
FeeType.Add(new FeeModel
{
FeeID = fees[i].FeeID,
FeeName = fees[i].FeeName,
FeeAmount = fees[i].FeeAmount,
IsFineApplicable = fees[i].IsFineApplicable,
LastDate = fees[i].LastDate,
});
trm.FeeDetails.ToList().AddRange(FeeType);
}
}
}
}
}
trm.FeeDetails.ToList().AddRange(FeeType);
You've created a new List<FeeModel>, added a copy of the FeeDetails sequence, added a new FeeModel object, and then thrown the new list away.
You need to store the modified list in the property - for example:
foreach (TermModel trm in acm.TermDetails)
{
if (trm.TermID == fees[i].TermID)
{
List<FeeModel> FeeType = new List<FeeModel>();
FeeType.Add(new FeeModel
{
FeeID = fees[i].FeeID,
FeeName = fees[i].FeeName,
FeeAmount = fees[i].FeeAmount,
IsFineApplicable = fees[i].IsFineApplicable,
LastDate = fees[i].LastDate,
});
FeeType.AddRange(trm.FeeDetails);
trm.FeeDetails = FeeType;
}
}

Automapper override values which are not in source

Hi have a problem with auomapper where i try to use the Automapper.Mapper(src, dest) without loosing the values in the dest after doing the mapping.
I have a class which has a list of objects like below
public class UpdateShipmentDetailDto
{
public bool IsDocument { get; set; }
public List<UpdateItemDetailDto> ItemDetails { get; set; } = new();
}
which i want to map to
public class SCS_OUT_Manifest
{
public Guid ManifestId { get; set; }
public ICollection<SCS_OUT_ManifestItem> SCS_OUT_ManifestItems { get; set; } = new List<SCS_OUT_ManifestItem>();
}
The UpdateItemDetailDto class looks like this
public class UpdateItemDetailDto
{
public Guid ItemId { get; set; }
public string ItemDescription { get; set; }
public int Qty { get; set; }
public Guid UnitsId { get; set; }
public decimal ItemValue { get; set; }
}
And the SCS_OUT_ManifestItem class looke like
public class SCS_OUT_ManifestItem
{
public Guid ItemId { get; set; }
public Guid ManifestId { get; set; }
public string ItemDescription { get; set; }
public int Qty { get; set; }
public Guid UnitsId { get; set; }
public decimal ItemValue { get; set; }
}
Im performing a maaping like below, which map from ItemDetails (which is a list) to SCS_OUT_ManifestItems (which is also a ICollection).
_mapper.Map(updateShipmentDetailDto.ItemDetails, manifest.SCS_OUT_ManifestItems);
The problem after mapping is done the properties which in the destination collection are set to the default values.
for example the ManifestId inthe SCS_OUT_ManifestItem manifest.SCS_OUT_ManifestItems which is not in updateShipmentDetailDto.ItemDetails is set to its default Guid value 00000000-0000-0000-0000-000000000000.
But if i run this in a loop like below it works.
foreach (var item in manifest.SCS_OUT_ManifestItems)
{
_mapper.Map(updateShipmentDetailDto.ItemDetails.Single(s => s.ItemId == item.ItemId), item);
}
Please help! thanks in advance
Try map your lists and your itens and use the same name ÏtemDetails" in both lists.
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<UpdateItemDetailDto, SCS_OUT_ManifestItem>();
cfg.CreateMap<UpdateShipmentDetailDto, SCS_OUT_Manifest>();
});
var _mapper = config.CreateMapper();
var updateShipmentDetailDto = new UpdateShipmentDetailDto();
var updateItemDetailDto = new UpdateItemDetailDto();
var manifest = new SCS_OUT_Manifest();
updateItemDetailDto.ItemId = Guid.NewGuid();
updateItemDetailDto.UnitsId = Guid.NewGuid();
manifest.ManifestId = Guid.NewGuid();
updateItemDetailDto.ItemDescription = "test";
updateItemDetailDto.Qty = 10;
updateItemDetailDto.ItemValue = 25.50M;
updateShipmentDetailDto.ItemDetails = new List<UpdateItemDetailDto>();
updateShipmentDetailDto.ItemDetails.Add(updateItemDetailDto);
_mapper.Map(updateShipmentDetailDto.ItemDetails, manifest.ItemDetails);
Console.WriteLine($"DTO Guid: {updateShipmentDetailDto.ItemDetails[0].ItemId}, Desc: {updateShipmentDetailDto.ItemDetails[0].ItemDescription}");
foreach (var item in manifest.ItemDetails)
{
Console.WriteLine($"Guid: {item.ItemId}, Desc: {item.ItemDescription}");
}
Console.WriteLine($"Guid Manifest: {manifest.ManifestId}");

setting value of a property in source list

I have 2 lists
public class EmailDetails
{
public int EmailMasterID { get; set; }
public string Name { get; set; }
public string Body { get; set; }
public string Number { get; set; }
public string IsModified { get; set; }
}
public class EmailDetailsActual
{
public string ProgramNumber { get; set; }
public string IsModified { get; set; }
public int EmailMasterID_FK { get; set; }
}
I need to set value of IsModified column to YES in EmailDetails list if EmailMasterID = EmailMasterID_FK (from EmailDetailsActual list) . If not, then set value to NO. The final result should be EmailDetails list.
im not sure but i put new EmailDetailsActual in EmailDetails and I put the details for that (ProgramNumber , IsModified , EmailMasterID_FK)
and then I put input to when Call EmailDetails , should fill inputs like
EmailDetails p1 = new EmailDetails("ProgramNumber", "IsModified", 0000);
after i put IsModified Get properties in EmailDetails >>
if (EmailMasterID == EDA.EmailMasterID_FK)
{
return "yes";
}
else
{
return "no";
}
// EDA is new EmailDetailsActual
And I accessed it values ​​in a this way (accessed EmailMasterID_FK (we create new EmailDetailsActual ) )
and its my finally >>>
public class EmailDetails
{
EmailDetailsActual EDA = new EmailDetailsActual();
public EmailDetails(string ProgramNumber , string IsModified , int EmailMasterID_FK)
{
EDA.ProgramNumber = ProgramNumber;
EDA.IsModified = IsModified;
EDA.EmailMasterID_FK = EmailMasterID_FK;
}
public int EmailMasterID { get; set; }
public string Name { get; set; }
public string Body { get; set; }
public string Number { get; set; }
public string IsModified { get
{
if (EmailMasterID == EDA.EmailMasterID_FK)
{
return "yes";
}
else
{
return "no";
}
}
}
}
public class EmailDetailsActual
{
public string ProgramNumber { get; set; }
public string IsModified { get; set; }
public int EmailMasterID_FK { get; set; }
}
this is example to work >>
EmailDetails p1 = new EmailDetails("ProgramNumber", "IsModified",9991);
p1.EmailMasterID = 9992;
Console.WriteLine(p1.IsModified);
its output no bc EmailMasterID (9991) its not same with EmailMasterID_FK(9992)
I hope I able to help you :)

Entity Framework to List without specific Column

I'm actually using EF and very new to it. I have a EDMX-Model from Database. I have to get a List of Entities, but in this Table is Binary-Column. I want my Object-List from that Table without the Binary Data.
My Entity-Objekt looks like:
public partial class bons
{
public int id { get; set; }
public System.DateTime zeitstempel { get; set; }
public byte[] bonBinaer { get; set; }
public Nullable<int> tisch_nr { get; set; }
public bool abgearbeitet { get; set; }
public int kunde_id { get; set; }
public string hash { get; set; }
public string dateiname { get; set; }
public string tisch_name { get; set; }
public int gang { get; set; }
public decimal brutto { get; set; }
public Nullable<System.DateTime> zeitstempelAbgearbeitet { get; set; }
public Nullable<int> positionenAnzahl { get; set; }
public bool manuell { get; set; }
}
And I#m getting the List like:
internal static List<bons> holeBongListeNichtAbgearbeitetRestaurant(int kunde_id)
{
List<bons> rückgabe = new List<bons>();
using (bonsEntities context = new bonsEntities())
{
rückgabe = context.bons.Where(x => x.kunde_id == kunde_id && x.abgearbeitet == false).OrderBy(x => x.zeitstempel).ToList();
}
return rückgabe;
}
Can someone help how to get the List without the 'byte[] bonBinaer'?
Hi you can use autoMapper with EF extension
like: https://docs.automapper.org/en/stable/Queryable-Extensions.html
or if you prefer you can do by yourself with a Select() method like:
using (bonsEntities context = new bonsEntities())
{
rückgabe = context.bons.Where(x => x.kunde_id == kunde_id && x.abgearbeitet == false).OrderBy(x => x.zeitstempel).Select(xx=> new {
id = xx.id
//and all you props
}).ToList().Select(yy=> new bons{
id=yy.id
//and back with other props
}).ToList();
}

Automapper: object of joined rows to header objects with a list of detail objects

I have a stored procedure that returns rows consisting of a join on Po header and Po Detail tables (so Po Header info is repeated in each child Po detail). My repository method maps this combined info into a Po object where each header info is repeated for every associated PO detail. Is there a best way to map these Po objects into a PO Header with a list of PO details?
Edit: I'm using Dapper for ORM
Second Edit: I added my repo method
public class Po
{
public string HeaderField1 { get; set; }
public string HeaderField2 { get; set; }
public int HeaderField3 { get; set; }
public string DetailField1 { get; set; }
public string DetailField2 { get; set; }
public int DetailField3 { get; set; }
}
to
public class Po_Header
{
public string HeaderField1 { get; set; }
public string HeaderField2 { get; set; }
public int HeaderField3 { get; set; }
List<Po_Detail> PoDetails { get; set; }
}
public class Po_Detail
{
public string DetailField1 { get; set; }
public string DetailField2 { get; set; }
public int DetailField3 { get; set;
}
Repo Method:
public IEnumerable<Po> GetAllPosByCustomer(string customerId)
{
try
{
using (var connection = (SqlConnection) _dbFactory.GetConnection())
{
var query = "dbo.spPo_GetAllByCustomerId";
var list = SqlMapper.Query<Po>(connection, query, param: new { #CustomerId = customerId }, commandType: CommandType.StoredProcedure);
return list;
}
}
catch(Exception e)
{
var err = e.ToString();
return null;
}
}

Categories

Resources