I have a list of subjects 'subjectList' I want to get the value based on two conditions like if subjectValue!=null then select subjectValue else if the subject is isDefault=true then select subjectDefaultCode, I have written an individual LINQ query for both condition that but I am not getting how to apply both the condition in a single query.?
Have a look.
string subjectValueDropDown = string.Empty;
First condition.
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.SubjectValue != null).Select(k => k.SubjectValue).ToArray());
Second condition.
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.IsDefault == true).Select(k => k.subjectDefaultCode).ToArray());
Possbilities:
Subject1 SubjectValue=null , isDefault=false
Subject2 SubjectValue=Maths , isDefault=false
Subject3 SubjectValue=null , isDefault=true
Subject4 SubjectValue=null , isDefault=false
In this situation or another situation where any of the subject have SubjectValue!=null then I don't want the isDefault condition to get executed.
Thanks in advance.
Updated:
Output should be same as op of below query:
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.SubjectValue != null).Select(k => k.SubjectValue).ToArray());
if(subjectValueDropDown==""){
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.IsDefault == true).Select(k => k.subjectDefaultCode).ToArray());
}
Is it possible to merge both the query into a single query to get the expected output.
Try this, it was tested in Visual Studio and values are the same as in your update:
subjectValueDropDown = string.Join(",",
subjectList
.Where(x =>
(x.SubjectValue != null)
|| (x.SubjectValue == null && x.IsDefault)
).Select(k =>
k.SubjectValue != null? k.SubjectValue : k.SubjectDefaultCode
)
.OfType<string>()
.ToArray());
using System.Collections.Generic;
using System.Linq;
namespace Game
{
class Subject
{
public string SubjectValue { get; set; }
public bool IsDefault { get; set; }
public string subjectDefaultCode = "Defauult code.";
}
class Program
{
static void Main(string[] args)
{
var subjectList = new List<Subject>
{
new Subject
{
IsDefault = false,
},
new Subject
{
SubjectValue = "Maths",
IsDefault = false,
},
new Subject
{
IsDefault = false,
},
new Subject
{
IsDefault = false,
},
};
var ubjectValueDropDown = string.Join(",", subjectList.Where(x => x.SubjectValue is not null || x.IsDefault)
.Select(x =>
{
if (x.SubjectValue is not null)
return x.SubjectValue;
if (x.IsDefault)
return x.subjectDefaultCode;
return "";
}));
}
}
}
Related
I cannot seem to get the desirable filtered result from my query.
Data
public class fdp_1115
{
public string Id{ get; set; }
public string Number{ get; set; }
public string Type{ get; set; }
}
List<fdp_1115> fdpList = new List<fdp_1115>
{
new fdp_1115 { Id = "1", Number = "Lot123", Type = "D14MWT" },
new fdp_1115 { Id = "2", Number = "Lot123", Type = "E12WBC7W1" }
};
List<string> searchValues = new List<string> { "MLE12WBC7W1 A R" };
LINQ:
var LocType = fdpList.FirstOrDefault(d => searchValues.Any(s => d.Type.Contains(s)));
if (LocType != null)
{
Console.WriteLine("Matching record found:");
Console.WriteLine($"Id: {LocType.Id}, Number: {LocType.Number}, Type: {LocType.Type}");
}
else
{
Console.WriteLine("No matching records found.");
}
The result I wanted is:
Matching record found:
Id: 2, Number: Lot123, Type: E12WBC7W1
But I got "No matching records found." which indicates that LocType == null.
I already tried trimming and ignoring case sensitive:
var LocType = fdpList.FirstOrDefault(d => searchValues.Any(s => d.Type.Contains(s.Trim().Replace(" ", ""))));
var LocType = fdpList.FirstOrDefault(d => searchValues.Any(s => d.Type.Contains(s, StringComparison.InvariantCultureIgnoreCase)));
But still no luck. Any idea how do I match "MLE12WBC7W1 A R" with "E12WBC7W1"?
You have your contains the other way around.
d.Type = "E12WBC7W1"
and
s = "MLE12WBC7W1 A R"
Then "E12WBC7W1" does not Contains "MLE12WBC7W1 A R"
It is the other way around.
var LocType = fdpList.FirstOrDefault(d => searchValues.Any(s => s.Contains(d.Type)));
Your current logic checks whether there is any object with Type value that contains the value for each string in the searchValues array.
From your requirement:
You want to filter the object that fulfills there is any string in searchValues containing the value of Type.
Thus it should be:
var LocType = fdpList.FirstOrDefault(d => searchValues.Any(s => s.Contains(d.Type)));
I would like to use this string as a filter to remove some Ids in a linq query
public class ProductKitMakerDto
{
public int Id { get; set; }
public string TitleShort { get; set; }
public string Media { get; set; }
}
[HttpPost]
public ActionResult KitItemSelect(string culture)
{
string productMakerIds = "4174,2196,2201,2460,2508,2204";
//create a list
var productMakerList = new List<ProductKitMakerDto>();
foreach (int i in productMakerIds)
{
productMakerList.Add(new ProductKitMakerDto { Id = i });
}
var itemselects = (from p in _context.Products
where p.Matrix == 2400
select new ProductKitMakerDto()
{
Id = p.Id,
TitleShort = culture == "de" ? p.TitleShortDe :
culture == "fr" ? p.TitleShortFr :
p.TitleShortEn,
Media = "/img/" + p.Photo,
}).ToList();
//From this query I get 40 results.
//Then I want to remove the ones from the list:
//itemselects = itemselects.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id));
//1st (above) I get an Error CS0266 asking for explicit cast. So aplly the modification
itemselects = (List<ProductKitMakerDto>)itemselects.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id));
return Json(itemselects, JsonRequestBehavior.AllowGet);
}
I get 500 (Internal Server Error) - xhr.send( options.hasContent && options.data || null );
I guess the list is empty.
Any idea? Thanks
this does not work
string productMakerIds = "4174,2196,2201,2460,2508,2204";
var productMakerList = new List<ProductKitMakerDto>();
foreach (int i in productMakerIds)
{
productMakerList.Add(new ProductKitMakerDto { Id = i });
}
because you need to split on comma first and parse the string to int:
foreach (string i in productMakerIds.Split(',')) // and parse i to int with int.Parse
but since it's a string literal, initialize it correctly in the first place. Don't use a List<ProductKitMakerDto> because you just need a List<int>, then you can use Contains:
var productMakerList = new List<int>
{
4174, 2196, 2201, 2460, 2508 , 2204
};
you can not cast to a list if it's not a list and Enumerable.Where does not return one:
itemselects = (List<ProductKitMakerDto>)itemselects.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id));
you need to append ToList after the Where
itemselects = itemselects
.Where(i => !productMakerList.Any(pml =>pml.Id == i.Id))
.ToList();
but as mentioned, you could also use this Where before you create that list the first time, so include the condition witha Contains which should be supported:
var itemselects = (from p in _context.Products
where p.Matrix == 2400
&& !productMakerList.Contains(p.Id)
select new ProductKitMakerDto()
{
Id = p.Id,
TitleShort = culture == "de"
? p.TitleShortDe
: culture == "fr" ? p.TitleShortFr : p.TitleShortEn,
Media = "/img/" + p.Photo,
}).ToList();
foreach (string i in productMakerIds.Split(','))
{
productMakerList.Add(new ProductKitMakerDto { Id = int.Parse(i) });
}
Is it possible to select two rows into one anonymous object DTO with two properties?
With a model like:
public class Document
{
public int Id { get; set; }
public string Text { get; set; }
// Other properties
}
I am writing a method that finds the difference between two versions of a document:
public Task<string> CompareVersions(int initialId, int finalId)
So I need to retrieve the text of exactly two Documents by Id, and I need know which was which.
Currently I am constructing a Dictionary<int, string> by doing:
var dto = await _context.Documents
.Where(doc => doc.Id == initialId
|| doc.Id == finalId)
.ToDictionaryAsync(x => x.Id, x => x.Text);
and then calling dto[initialId] to get the text. However, this feels very cumbersome. Is there any way to take the two Ids and select them into one DTO in the form
{
InitialText,
FinalText
}
You have to use SelectMany
var query =
from initial in _context.Documents
where initial.Id = initialId
from final in _context.Documents
where final.Id = finalId
select new
{
InitialText = initial.Text,
FinalText = final.Text
};
var result = await query.FirstOrDefaultAsync();
Aggregate can do it too
var dto = (await _context.Documents
.Where(doc => doc.Id == initialId || doc.Id == finalId).ToListAsync())
.Aggregate(
new { InitialText = "", FinalText = "" },
(seed, doc) => {
if(doc.Id == initialId)
seed.InitialText = doc.Text;
else
seed.FinalText = doc.Text;
}
);
I'm not sure I like it any more than I do your dictionary approach, but with an actual dto at the end rather than the dictionary:
var d = await _context.Documents
.Where(doc => doc.Id == initialId || doc.Id == finalId)
.ToDictionaryAsync(x => x.Id, x => x.Text);
var dto = new { InitialText = d[initialId], FinalText = d[finalId] };
You could also perhaps just:
var dto = new {
InitialText = await context.Documents
.FindAsync(initialId),
FinalText = await context.Documents
.FindAsync(finalId)
};
What I'm doing wrong in this method below? I created a group with linq because I need to group the list by 2 columns and for this grouping I will have a list of files.
[HttpGet]
[Route("versions-by-period")]
public IActionResult GetVersionsByPeriodId(int entityId, int periodId)
{
var versionsInvoiceBillet = db.RemittanceInvoiceBilletVersionsCompacts
.Where(x => x.LegalEntityId == entityId && x.PeriodId == periodId && x.IsCurrent && x.DownloadHash != null)
.GroupBy(x => new { x.LifePolicyNumber, x.LegalEntityGroupNumber },
i => new { i.DownloadHash, i.FileTypeEnum, i.DueDate }, (key, group) => new
{
LifePolicyNumber = key.LifePolicyNumber,
LegalEntityGroupNumber = key.LegalEntityGroupNumber,
Files = group.ToList()
});
return Ok(versionsInvoiceBillet.Select(x => new {
lifePolicyNumber = x.LifePolicyNumber,
legalEntityGroupNumber = x.LegalEntityGroupNumber,
invoiceAndBillet = x.Files.Select(f => new {
downloadHash = f.DownloadHash,
fileTypeEnum = f.FileTypeEnum,
dueDatet = f.DueDate
})
}));
}
If I try to call this method with Postman, the body comes empty. The problem is in invoiceAndBillet information that is returned, if I change to below, the body comes filled.
return Ok(versionsInvoiceBillet.Select(x => new {
lifePolicyNumber = x.LifePolicyNumber,
legalEntityGroupNumber = x.LegalEntityGroupNumber,
invoiceAndBillet = x.Files.Select
}));
If I try to debug the selection that I'm trying to return, I get this message below:
I have been handed over an application that uses entity framework. I'm not familiar with entity and I'm having an issue that I can't figure out. This application was made to migrate data from a database to a more relational database. After the initial migration, we have to run it again to insert additional rows that were not part of the original migration. (There is a 3 week gap). I know that I have to put a check in and I want to do this by one of the columns we uses named "DateChanged" but unfortunately I'm not sure how to do this in entity. This is my first effort and it just shows in red which is depressing. I have searched on the internet but have found no solutions.
if (!newData.tVehicleLogs.Any(v => v.DateChanged.Value.ToShortDateString("6/27/2014")))//I'm not sure how to check the DateChanged here.
{
newData.tVehicleLogs.Add(deal);
comment = new tVehicleComment
{
Comment = vehicle.Reason,
DealID = deal.DealID,
CurrentComment = false
};
newData.tVehicleComments.Add(comment);
newData.SaveChanges();
int cId = comment.CommentID;
deal.CommentID = cId;
}
}
So as you can see I'm trying to check the date with the if statement, but I can't get the syntax correct... after trying everything I know to try .. which isn't much at this point.
I basically need to check if the DateChanged is from 6/27/2014 to today's date. If it's before then, then it has already been migrated over and doesn't need migrated over again. Where it says comment, if the row is new, then it inserts the old comment into the new comments table, then updates the tVehicleLogs table with the commentID. I'm just stuck on the date checking part. Any help is greatly appreciated!!
EDIT: This is the entire code for inserting the into tVehicleLogs..
if (MigrateLogs)
{
List<VLog> vlog = oldData.VLogs.ToList();
foreach (VLog vehicle in vlog)
{
tBank bank;
tCustomer cust;
tFIManager manag;
tSalesPerson sales;
tMake make;
tModel model;
tDealership dealership;
tMakeDealership makedeal;
tVehicleComment comment;
tInternalLocation location;
string dealershipName = getProperDealershipName(vehicle.Dealership, newData);
bank = (newData.tBanks.Any(banks => banks.BankName == vehicle.BankName) ? newData.tBanks.Where(b => b.BankName == vehicle.BankName).FirstOrDefault() : newData.tBanks.Add(new tBank { BankName = vehicle.BankName }));
cust = (newData.tCustomers.Any(customer => customer.CustomerNumber == vehicle.CustNumber) ? newData.tCustomers.Where(customer => customer.CustomerNumber == vehicle.CustNumber).FirstOrDefault() : newData.tCustomers.Add(new tCustomer { CustomerNumber = vehicle.CustNumber, CustomerName = vehicle.Buyer }));
//cust = (newData.tCustomers.Any(customer => customer.CustomerNumber == vehicle.CustNumber && customer.CustomerName == vehicle.CustNumber) ? newData.tCustomers.Where(customer => customer.CustomerNumber == vehicle.CustNumber).FirstOrDefault() : newData.tCustomers.Add(new tCustomer { CustomerNumber = vehicle.CustNumber, CustomerName = vehicle.Buyer }));
manag = (newData.tFIManagers.Any(manager => manager.FIName == vehicle.FIName) ? newData.tFIManagers.Where(manager => manager.FIName == vehicle.FIName).FirstOrDefault() : newData.tFIManagers.Add(new tFIManager { FIName = vehicle.FIName }));
sales = (newData.tSalesPersons.Any(person => person.SalesPersonNumber == vehicle.SalesPerson) ? newData.tSalesPersons.Where(person => person.SalesPersonNumber == vehicle.SalesPerson).FirstOrDefault() : newData.tSalesPersons.Add(new tSalesPerson { SalesPersonNumber = vehicle.SalesPerson }));
make = (newData.tMakes.Any(m => m.Make == vehicle.Make) ? newData.tMakes.Where(m => m.Make == vehicle.Make).FirstOrDefault() : newData.tMakes.Add(new tMake { Make = vehicle.Make }));
model = (newData.tModels.Any(m => m.Model == vehicle.Model) ? newData.tModels.Where(m => m.Model == vehicle.Model).FirstOrDefault() : newData.tModels.Add(new tModel { Model = vehicle.Model, MakeID = make.MakeID }));
dealership = (newData.tDealerships.Any(d => d.DealershipName == dealershipName) ? newData.tDealerships.Where(d => d.DealershipName == dealershipName).FirstOrDefault() : newData.tDealerships.Add(new tDealership { DealershipName = dealershipName }));
makedeal = (newData.tMakeDealerships.Any(d => d.MakeID == make.MakeID && d.DealershipID == dealership.DealershipID) ? newData.tMakeDealerships.Where(d => d.MakeID == make.MakeID && d.DealershipID == dealership.DealershipID).FirstOrDefault() : newData.tMakeDealerships.Add(new tMakeDealership { DealershipID = dealership.DealershipID, MakeID = make.MakeID }));
location = (newData.tInternalLocations.Any(l => l.LocationName == vehicle.Location) ? newData.tInternalLocations.Where(l => l.LocationName == vehicle.Location).FirstOrDefault() : newData.tInternalLocations.Add(new tInternalLocation { LocationName = vehicle.Location }));
//log = (newData.tVehicleLogs.Any(l => l.DealNumber == vehicle.FIMAST &&) ? newData.tVehicleLogs.Where(l => l.DealNumber == vehicle.FIMAST).FirstOrDefault() : newData.tVehicleLogs.Add(new tVehicleLog {DealNumber = vehicle.FIMAST }));
Int32 stat;
int? status;
if (Int32.TryParse(vehicle.Status, out stat))
status = stat;
else
status = null;
DateTime titled, bounced, dateReceived;
bool trueTitled = DateTime.TryParse(vehicle.Titled, out titled);
bool trueBounced = DateTime.TryParse(vehicle.Bounced, out bounced);
bool trueReceived = DateTime.TryParse(vehicle.DateReceived, out dateReceived);
int dealid = newData.tVehicleDeals.Where(v => v.DealNumber == vehicle.FIMAST).FirstOrDefault().DealID;
tVehicleLog deal = new tVehicleLog
{
DealNumber = vehicle.FIMAST,
StockNumber = vehicle.StockNumber,
BankID = bank.BankID,
CustomerID = cust.CustomerID,
FIManagerID = manag.FIManagerID,
SalesPersonID = sales.SalesPersonID,
VINNumber = null,
DealDate = vehicle.DealDate,
NewUsed = vehicle.NewUsed,
GrossProfit = vehicle.GrossProfit,
AmtFinanced = vehicle.AmtFinanced,
CloseDate = null,
Category = vehicle.RetailLease,
Status = status,
DealershipID = dealership.DealershipID,
NewDeal = false,
Archived = false,
InternalLocationID = location.InternalLocationID,
ChangedBy = vehicle.ChangedBy,
DateChanged = DateTime.Parse(vehicle.DateChanged),
Titled = null,
Bounced = null,
MakeID = make.MakeID,
ModelID = model.ModelID,
DealID = dealid,
CommentID = null
};
if (trueTitled)
deal.Titled = titled;
if (trueBounced)
deal.Bounced = bounced;
if (trueReceived)
deal.DateReceived = dateReceived;
DateTime targetDate = new DateTime(2014, 06, 27);
//if(!newData.tVehicleLogs.Any(v => v.DateChanged >= targetDate))
if(deal.DateChanged >= targetDate && !newData.tVehicleLogs.Any(v => v.DateChanged >= targetDate))
{
newData.tVehicleLogs.Add(deal);
comment = new tVehicleComment
{
Comment = vehicle.Reason,
DealID = deal.DealID,
CurrentComment = false
};
newData.tVehicleComments.Add(comment);
newData.SaveChanges();
int cId = comment.CommentID;
deal.CommentID = cId;
}
}
}
I don't think you need to use linq here (providing you've pulled the object down). Just check the dates.
// pull down the object
var deal = newData.tVehicleLogs.Where(v => v.Id == SOMEID).FirstOrDefault();
DateTime targetDate = new DateTime(2014,06,27);
if (tVehicleLogs.DateChaned <= DateTime.Now
&& tVehicleLogs.DateChaned >= targetDate) {
}
Alternatively, pull down all the objects that meet the date criteria and foreach over them.
List<YourObject> list = newData.tVehicleLogs.Where(v => v.DateChanged <= DateTime.Now
&& v.DateChanged >= targetDate).ToList();
foreach(var l in list) {
// do your stuff here
}