Linq code have errors when i use Distinct() - linq

Guys I am working on a code and I need to do a distinct selecting just 2 columns. Can you guys tell me what is wrong with this code ?
foreach (var lbd in (from lbdrow in Db.LaborDtl
where (lbdrow.Company==epged.Company && epged.EmpID==lbdrow.EmployeeNum && lbdrow.PayrollDate==epged.PayrollDate)
select new {Company==lbdrow.Company && ProjectID==lbdrow.ProjectID})).Distinct()

Guys my apologies for the stupid question i found the solution
foreach (var lbd in (from lbdrow in Db.LaborDtl
where (lbdrow.Company==epged.Company && epged.EmpID==lbdrow.EmployeeNum && lbdrow.PayrollDate==epged.PayrollDate)
select new {Company==lbdrow.Company && ProjectID==lbdrow.ProjectID}).Distinct())

Related

Does the second from clause in this LINQ query do anything?

I am trying to simplify a C# method that includes the following query:
var matchingDeviceKeys =
from dev in data.Elements("Key1")
where dev.Element("Key2").Element("Key3").Element("VendorID").Value == device.Vendor &&
dev.Element("Key2").Element("Key3").Element("ProductType").Value == device.ProductType &&
dev.Element("Key2").Element("Key3").Element("ProductCode").Value == device.ProductCode
from rev in dev.Element("Key2").Element("Key3").Element("Key4").Elements("MajorRev")
where rev.Attribute("Number").Value == device.MajorRevision &&
rev.Attribute("DefaultMinorRev").Value == device.MinorRevision.ToString(CultureInfo.InvariantCulture)
select dev.Element("Key6").Element("Key7").Element("Key8");
The "rev" target for the second from clause is never used in the select line, making me wonder if that entire second from clause is doing anything. If it is doing something, what is it doing?
My apologies for obfuscating the element names. I have to be careful in sharing proprietary code.

How to make efficient search with LINQ in MVC4? [duplicate]

This question already has answers here:
entity framework: conditional filter
(3 answers)
Closed 6 years ago.
Hi I am developing MVC4 application. I am developing one search page which contains 5 textboxes and search button in which only one textbox is mandatory field. Remaining are not mandatory. I want to have AND of all 5 textbox values. My query is as below.
var logDetails = (from c in db.ts_upldlog_content
join tbl in db.ts_upld_doc on c.upld_docid equals tbl.upld_docid
join doc in db.tm_doc_type on tbl.upld_doctypeid equals doc.doc_typeid
where
(tbl.upld_clientid == clientId && tbl.upld_employeeid == employeeID && tbl.upld_empcitizenid == citizenId && tbl.upld_doctypeid == typeofDocument && EntityFunctions.TruncateTime(c.updatedOn) >= start && EntityFunctions.TruncateTime(c.updatedOn) < end)
select new logdetails
{
//Getting all properties
}).toList();
My problem is In the above query start and end are mandatory so i will get some value from UI. Let me explain one scenario. User will supply start and end and remaining will be null. My query will not yield results because I am doing && operation and my other fields may have some value in DB.
Lets conside below table.
clientID EmployeeID EmpCitiID docType Date
123 456 456 1 10/19/2016
When i Pass only Date my query will not work because I am doing && operation. so Is there any way to achieve this scenario? Any help would be appreciated. Thank you.
tbl.upld_clientid == clientId && tbl.upld_employeeid == employeeID && tbl.upld_empcitizenid == citizenId && tbl.upld_doctypeid == typeofDocument &&
(EntityFunctions.TruncateTime(c.updatedOn) >= start && EntityFunctions.TruncateTime(c.updatedOn) < end ) )
missing parenthesis

How can i use LINQ with property

Hi I have a array list if type class "DtContract".
ArrayList listOfContracts_;
foreach (DTContract contract in listOfContracts_)
{
if (contract.Engine != DTIsland.EngineType.AMADEUS && contract.Engine !=DTIsland.EngineType.SABRE)
continue;
}
I want to do it through LINQ.
I want to filter the Contract whose EngineType == AMADEUS && EngineType == SABRE. Please suggest how can i do it through Linq and get the result in List or in array list.
I am doing this to Optimize the code.
Please Help...
var result = listOfContracts_.Where(contract=>contract.Engine != DTIsland.EngineType.AMADEUS && contract.Engine !=DTIsland.EngineType.SABRE).ToList();
your foreach loop doen't do anything meaningful, what you are trying achieve?
If you want to use linq
listOfContracts_.OfType<DTContract>()
.Where(contract => contract.Engine != DTIsland.EngineType.AMADEUS &&
contract.Engine != DTIsland.EngineType.SABRE);

Order LINQ by "string" name

PROBLEM SOLVED!!!
The solution is Linq.Dynamic
You do it like this:
(from c in Context.AccountCharts
where c.Account_FK == account && c.Year_FK == year select c).OrderBy(order);
You have to download the System.Linq.Dynamic.dll and include it into your project.
Is there a way to order a linq query by the name of a field. like this:
from c in Context.AccountCharts
where c.Account_FK == account && c.Year_FK == year
orderby c["ColName"] select c;
Or
from c in Context.AccountCharts
where c.Account_FK == account && c.Year_FK == year
orderby c.GetType().GetField("ColName") select c;
None of these two works but I hope you know of a way to do this.
I spend to many time to resolve this issue, but didn't find any better than:
var queryExpression;
if (c["ColName"]=="CreateDate")
queryExpression.OrderBy(x => x.CreateDate);
Hope this will work too though not tested
Context.AccountCharts.Where(c=>c.Account_FK == account && c.Year_FK == year).OrderBy(o=>o.order);

problem with delete when multiple columns

when deleteing this works:
orderitems.Delete(x => x.orderitem_sessionid == transkey);
however this does not work
orderitem.Delete(x => x.orderitem_sessionid == transkey
&& x.orderitem_productid == 6);
i get no errors, but nothing is deleted either, i have working code as a substitute of
var DeleteableItems = orderitems.All().where(x => x.orderitem_sessionid == transkey
&& x.orderitem_productid = 6);
foreach(var item in DeleteableItems) item.delete;
though the above works it still bugs me that this wont work with just nomal delete method, using subsonic 3.0.0.3 and mysql database
thanks
Have you tried:
orderitem.Delete(x => x.orderitem_sessionid == transkey
&& x.orderitem_productid == 6);
The last operator in your version is assignment not comparisson.
My answer concerned a typo but the comments on getting the generated SQL below some might find useful.
Kindness,
Dan

Resources