convert lambda expression to function - c#

I wish to debug lambda expression but its not possible as it sets breakpoint on whole expression.
Here is my lambda expression
public bool CanRevert => _objectEntity != null && !(_objectEntity != null &&
ObjectDescription == _objectEntity.DTO.Description &&
ObjectInstance == _objectEntity.DTO.ObjectInstance.ToString() &&
Name == _objectEntity.DTO.ObjectName &&
(SelectedDevice != null && SelectedDevice.Id == _objectEntity.DTO.DeviceID) &&
(_objectEntity.DTO.ObjectCategoryID.HasValue ? (SelectedObjectCategory != null && SelectedObjectCategory.Id == _objectEntity.DTO.ObjectCategoryID.Value) : SelectedObjectCategory == null) &&
(_objectEntity.DTO.ObjectTypeID.HasValue ? (SelectedObjectType != null && SelectedObjectType.Id == _objectEntity.DTO.ObjectTypeID.Value) : SelectedObjectType == null));
I wanted to put breakpoint inside selectedDevice but i cant. Hence i tried to write it like
public bool CanRevert()
{
if (SelectedDevice != null)
{
var s = SelectedDevice.Id == _objectEntity.DTO.DeviceID;
}
var d = _objectEntity != null && !(_objectEntity != null &&
ObjectDescription == _objectEntity.DTO.Description &&
ObjectInstance == _objectEntity.DTO.ObjectInstance.ToString() &&
Name == _objectEntity.DTO.ObjectName &&
(SelectedDevice != null && SelectedDevice.Id == _objectEntity.DTO.DeviceID) &&
(_objectEntity.DTO.ObjectCategoryID.HasValue ? (SelectedObjectCategory != null && SelectedObjectCategory.Id == _objectEntity.DTO.ObjectCategoryID.Value) : SelectedObjectCategory == null) &&
(_objectEntity.DTO.ObjectTypeID.HasValue ? (SelectedObjectType != null && SelectedObjectType.Id == _objectEntity.DTO.ObjectTypeID.Value) : SelectedObjectType == null));
return d;
}
Is it right way to convert it? I ask because i am starting to get somewhere in application that Binding to this method is not supported

You can achieve the same result like this.
Put a breakpoint in your expression.
Start Debugging (F5)
Highlight (SelectedDevice != null && SelectedDevice.Id == _objectEntity.DTO.DeviceID) in your expression
Right-click and select Add Watch
Now you have its calculated value in the Watch window. (Usually pops up at the bottom)
You can use Quick Watch (Shift+F9) if you want its value once.

Related

How can I pull a repeated where clause expression from linq into a function?

I have a large project where I have dozens of linq statements where I am looking for a matching record by checking several fields to see if they match or both field and compared field are null.
var testRecord = new { firstField = "bob", secondField = (string)null, thirdField = "ross" };
var matchRecord = dataContext.RecordsTable.FirstOrDefault(vi =>
(vi.first == testRecord.firstField || ((vi.first == null || vi.first == string.Empty) && testRecord.firstField == null))
&& (vi.second == testRecord.secondField || ((vi.second == null || vi.second == string.Empty) && testRecord.secondField == null))
&& (vi.third == testRecord.thirdField || ((vi.third == null || vi.third == string.Empty) && testRecord.thirdField == null)));
//do stuff with matchRecord
Ideally I would replace all that duplicated code (used around 50 times across the system I'm working on) with something like the following
Expression<Func<string, string, bool>> MatchesOrBothNull = (infoItem, matchItem) => (
infoItem == matchItem || ((infoItem == null || infoItem == string.Empty) && matchItem == null));
var matchRecord = dataContext.RecordsTable.FirstOrDefault(vi =>
MatchesOrBothNull(vi.first, testRecord.firstField)
&& MatchesOrBothNull(vi.second, testRecord.secondField)
&& MatchesOrBothNull(vi.third, testRecord.thirdField));
//do stuff with matchRecord
My question is two-fold: First, is there a matched or both null function already available? (I've looked without luck).
Second, the code block above compiles, but throws a "no supported translation to sql" error, is there a way to have a function in the where clause? I know that there is a translation because it works if I don't pull it into the function. How can I get that translated?
First of all you can check whether string is null or empty with single code which is called : String.IsNullOrEmpty(vi.first). You need a method like this one :
public bool MatchesOrBothNull(string first,string second){
if(first==second||String.IsNullOrEmpty(first)||String.IsNullOrEmpty(second))
return true;
else return false;
}
You can use it in where clause
var matchRecord = dataContext.RecordsTable.Where(vi =>
MatchesOrBothNull(vi.first, testRecord.firstField)
&& MatchesOrBothNull(vi.second, testRecord.secondField)
&& MatchesOrBothNull(vi.third, testRecord.thirdField)
).FirstOrDefault();

Any way to further reduce the number of lines of code?

I'm looking for ways to reduce the number of lines of code for this function. Any help is appreciated!
private bool CanSubmitPackage ( object obj )
{
// ---- Checks if the package contains any files to be submitted ----
if ( _selectedWorkspace.Package.Files != null )
{
if ( _selectedWorkspace.Package.Files.Count > 0 )
if ( _selectedWorkspace.Package.Platform != null || _selectedWorkspace.Package.Platform != "" )
if ( _selectedWorkspace.Package.PackagePath != null || _selectedWorkspace.Package.PackagePath != "" )
if ( _selectedWorkspace.Package.PackageSize != null || _selectedWorkspace.Package.PackageSize != "" )
if ( _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "" )
return true;
else
return false;
else
return false;
else
return false;
else
return false;
else
return false;
}
else
return false;
}
return ( _selectedWorkspace.Package.Files?.Count > 0
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));
You can make it shorter and readable by using && operator and string.IsNullOrEmpty method:
return ( _selectedWorkspace.Package.Files != null
&& _selectedWorkspace.Package.Files.Count > 0
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));
If you're using c#6+, then you can go further (just a little bit) and use Expression Bodied method:
private bool CanSubmitPackage () => //I'm not sure about object obj argument
( _selectedWorkspace.Package.Files != null
&& _selectedWorkspace.Package.Files.Count > 0
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));
If you are going after making your code text shorted, you can declare a short named variable and use that instead of _selectedWorkspace.Package:
var p = _selectedWorkspace.Package;
return (p.Files?.Count > 0
&& !string.IsNullOrEmpty(p.Platform)
&& !string.IsNullOrEmpty(p.PackagePath)
&& !string.IsNullOrEmpty(p.PackageSize)
&& !string.IsNullOrEmpty(p.SubmittedBy));;
This is one way to do it
Update
return ( _selectedWorkspace != null
&& _selectedWorkspace.Package != null
&& _selectedWorkspace.Package.Files != null
&& _selectedWorkspace.Package.Files.Count > 0
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));
Old Ans
return ( _selectedWorkspace.Package.Files != null
&& _selectedWorkspace.Package.Files.Count > 0
&& ( _selectedWorkspace.Package.Platform != null || _selectedWorkspace.Package.Platform != "" )
&& ( _selectedWorkspace.Package.PackagePath != null || _selectedWorkspace.Package.PackagePath != "" )
&& ( _selectedWorkspace.Package.PackageSize != null || _selectedWorkspace.Package.PackageSize != "" )
&& ( _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "" ));
This is another way to do it
private bool CanSubmitPackage(object obj)
{
// ---- Checks if the package contains any files to be submitted ----
if (_selectedWorkspace.Package.Files == null) return false;
if (_selectedWorkspace.Package.Files.Count <= 0) return false;
if (_selectedWorkspace.Package.Platform == null && _selectedWorkspace.Package.Platform == "") return false;
if (_selectedWorkspace.Package.PackagePath == null && _selectedWorkspace.Package.PackagePath == "") return false;
if (_selectedWorkspace.Package.PackageSize != _selectedWorkspace.Package.PackageSize != "")
return _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "";
return false;
}
I would prefer #xneg's answer (https://stackoverflow.com/a/53096740/10588170), however, you could also drop all 'else' Statements and replace them with a single return statement after the 'if'-block.
Make sure to use string.IsNullOrEmpty(_str) in future, or you have to use && instead of ||, otherwise you will get an Exception because you are checking if _str is empty if it is null and if it is not null, you are not checking the content of _str at all. I am pretty sure that that's not what you wanna do.
I would maybe split the conditions, for example:
public bool HasFile
{
get { return _selectedWorkspace.Package.Files != null && _selectedWorkspace.Package.Files.Count > 0; }
}
public bool HasPlatform
{
get { return !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform); }
}
// Do that for all conditions
And then you can write your final if like this:
bool result = HasFile && HasPlatform && …
If you wanted to keep it largely the same, as the return for all of your IF statements failing is false, you only need to include it once. If any of your current checks fail, it won't be able to execute "return true", so you can just have one final return statement like this:
private bool CanSubmitPackage ( object obj )
{
// ---- Checks if the package contains any files to be submitted ----
if ( _selectedWorkspace.Package.Files != null )
if ( _selectedWorkspace.Package.Files.Count > 0 )
if ( _selectedWorkspace.Package.Platform != null || _selectedWorkspace.Package.Platform != "" )
if ( _selectedWorkspace.Package.PackagePath != null || _selectedWorkspace.Package.PackagePath != "" )
if ( _selectedWorkspace.Package.PackageSize != null || _selectedWorkspace.Package.PackageSize != "" )
if ( _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "" )
return true;
// One of the above checks failed, that's why we're running this line
return false;
}
I also prefer #xneg, #SeM, or #Mihir Dave's approach (as they're all basically the same answer), but it does come down to what you find most comfortable / readable. I would definitely use the method String.IsNullOrEmpty() to simplify your if statements however.
Additionally, what the ? does in xneg's answer: _selectedWorkspace.Package.Files?.Count is say "If Files is not null, retrieve Files.Count, otherwise return null. Then in C# a statement like, null > 0 always returns false.

c# Multiple Independent Filters on Lambda Query

I've been working on this for hours today and feel like there is an easy way to do this but I am unable to make it work by anything but brute force.
I have an entity in my application that serves as a Mapping between two objects, based on 5 filters. The goal is to find the record with the most specific match to the filters.
Right now, I am manually brute forcing 2^5 queries to get the most specific row, but feel like there has to be a much easier way to do this.
The only gotcha here is that there may be no match in the database for a specific filter (or all), in which case I want to select the NULL record.
Below I have an excerpt of my embarrassing brute force query -- I first want to try and match on all 5 filters, then on permutations of 4 matching, then 3, then 2, then 1, and finally all Nulls.
incList.FirstOrDefault(x =>
x.Filter1 == filter1Parameter && x.Filter2 == filter2Parameter && x.Filter3 == filter3Parameter && x.Filter4 == filter4Parameter && x.Filter5 == filter5Parameter
|| x.Filter1 == null && x.Filter2 == filter2Parameter && x.Filter3 == filter3Parameter && x.Filter4 == filter4Parameter && x.Filter5 == filter5Parameter
|| x.Filter1 == filter1Parameter && x.Filter2 == null && x.Filter3 == filter3Parameter && x.Filter4 == filter4Parameter && x.Filter5 == filter5Parameter
|| x.Filter1 == filter1Parameter && x.Filter2 == filter2Parameter && x.Filter3 == null && x.Filter4 == filter4Parameter && x.Filter5 == filter5Parameter
|| x.Filter1 == filter1Parameter && x.Filter2 == filter2Parameter && x.Filter3 == filter3Parameter && x.Filter4 == null && x.Filter5 == filter5Parameter
|| x.Filter1 == filter1Parameter && x.Filter2 == filter2Parameter && x.Filter3 == filter3Parameter && x.Filter4 == filter4Parameter && x.Filter5 == null
I originally thought I could have a simple statement that would independently grab the value if present, and grab null if not
incList.FirstOrDefault(x => (x.Filter1 == filter1Parameter || x.Filter1 == null) &&
(x.Filter2 == filter2Parameter || x.Filter2 == null) &&
(x.Filter3 == filter3Parameter || x.Filter3 == null) &&
(x.Filter4 == filter4Parameter || x.Filter4 == null) &&
(x.Filter5 == filter5Parameter || x.Filter5 == null));
But that did not work.
Any pointers would be appreciated.
I'm still trying to understand the full extent of the requirement.
However, have you explored abstracting this Func or Func(s) in a separate class. This separate class or classes would operate like a Strategy. Only in charge of what filters a collection based on a certain predicate.
If that does not seem like a good route, what about looking into writing your own implementation IEqualityComparer. This will allow you determine what makes these objects equal.
You can introduce something like a "fitness function" and then select item with max fit value
var bestMatch = incList.Select(x => new
{
item = x,
fit =
(x.Filter1 == null
? 1
: (x.Filter1 == filter1Parameter ? 2 : 0)) *
(x.Filter2 == null
? 1
: (x.Filter2 == filter2Parameter ? 2 : 0)) // and so on
})
.OrderByDescending(x => x.fit)
.FirstOrDefault(x => x.fit > 0)?.item;

LINQ List gives null exception in where Clause

var q = from p in query
where
((criterias.birthday == p.BirthDay|| criterias.birthday == null))
&& ((criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate))
&& ((criterias.gender == p.Gender) || (criterias.gender == null))
&& ((criterias.nationalities.Contains(p.Nationality)) || (criterias.nationalities == null))
criterias isa class where i store my search criterias. nationalities is a string list. the problem occurs when i have no items in string. the query throws null reference exception. the query doesnt accept null value in nationalities. how can i fix this?
Reverse the order so that the null check comes before the query: as you're using ||, the second part of the expression is only evaluated when the first part evaluates to false:
&& ((criterias.nationalities == null) ||
(criterias.nationalities.Contains(p.Nationality)))
Look at these 2:
((criterias.birthday == p.BirthDay|| criterias.birthday == null))
&& ((criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate))
I don't think marriageDate will give you problems but birthday uses the wrong order.
In this case you need the 'short-circuit evaluation' property of ||, change it to:
(criterias.birthday == null || criterias.birthday == p.BirthDay)
&& (criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate)
Try swapping the order of the nationalties checks. It should short-circuit on the null check before it tries to evaluate the Contains.
((criterias.nationalities == null) || (criterias.nationalities.Contains(p.Nationality)))
Turn this statement around:
(criterias.nationalities.Contains(p.Nationality)) || (criterias.nationalities == null)
so that it reads
(criterias.nationalities == null) || (criterias.nationalities.Contains(p.Nationality))
If the first operand evaluates to true, the second one will be skipped.
Try first check for null then (if it is not null) check for contains:
var q = from p in query
where
((criterias.birthday == p.BirthDay|| criterias.birthday == null))
&& ((criterias.marriageDate == null || criterias.marriageDate == p.MarriageDate))
&& ((criterias.gender == p.Gender) || (criterias.gender == null))
&& ((criterias.nationalities == null) || (criterias.nationalities.Contains(p.Nationality))

LINQ Method Syntax - how to accomplish dynamic Linq statements

I use Linq to Entities to retrieve my records from DB. The function below is in a method. Method has some parameters (arguments) like group, datefrom, dateto, place, state , searchtext etc. etc.
the whole idea is if these parameters are not empty or null then accomplish the LINQ statments. The way I'm doing is I'm checking whether there is a value or not. if it has the value then I pass e.g. a.no_group= group if it doesn't has a value then I pass a statement like a.id!=-1, which is always true.
Problem:
I mean I'm not happy with passing in every statement like "a.id != -1" which is always true . I use this because I have to put a value there. But I'm not happy with this way of doing it... (it works!)
Question:
The question is: this right way of doing?
1- Can I replace a.id != -1 with something else ?
2- If you see the whole linq statement is duplicated because of language check. As you see the last
linq statement checks on language 'Dutch' or 'French'... How to avoid dupliation?
3- I'm checking whether the current date (datetime.now) is between date_begin and date_end. Is this correct way...
The whole code works fine, but I think I'm complicating the code which can be much simpler...
But how?
if (Language == ConfBouwHelper.LanguageEnum.French)
{
//FRENCH RECORDS
listAgendaItems = dc.agenda.Where(a =>
((String.IsNullOrEmpty(group)) ? (a.id != -1) : (a.no_group == group))
&& ((activityType.Equals("ALL")) ? (a.id != -1) : (a.type_manifestation == activityType))
&& ((String.IsNullOrEmpty(dateFrom)) ? (a.id != -1) : (a.date_debut.Value >= dateFrom))
&& ((String.IsNullOrEmpty(dateTo)) ? (a.id != -1) : (a.date_debut.Value <= dateTo))
&& ((String.IsNullOrEmpty(place)) ? (a.id != -1) : (a.emplacement.Contains(place)))
&& ((String.IsNullOrEmpty(state)) ? (a.id != -1) : (a.cd_prov == state))
&& ((String.IsNullOrEmpty(searchText)) ? (a.id != -1) : (a.libelle_activite.Contains(searchText)))
&& ((a.date_begin_display.HasValue ? DateTime.Now >= a.date_begin_display.Value : a.id != -1) &&
(a.date_end_display.HasValue ? DateTime.Now <= a.date_end_display.Value : a.id != -1))
&& (a.langue == "FRENCH" || a.langue == "B")).ToList<agenda>(); //GET FRENCH
}
else
//DUTCH RECORDS
{
listAgendaItems = dc.agenda.Where(a =>
((String.IsNullOrEmpty(group)) ? (a.id != -1) : (a.no_group == group))
&& ((activityType.Equals("ALL")) ? (a.id != -1) : (a.type_manifestation == activityType))
&& ((String.IsNullOrEmpty(dateFrom)) ? (a.id != -1) : (a.date_debut.Value >= dateFrom))
&& ((String.IsNullOrEmpty(dateTo)) ? (a.id != -1) : (a.date_debut.Value <= dateTo))
&& ((String.IsNullOrEmpty(place)) ? (a.id != -1) : (a.emplacement.Contains(place)))
&& ((String.IsNullOrEmpty(state)) ? (a.id != -1) : (a.cd_prov == state))
&& ((String.IsNullOrEmpty(searchText)) ? (a.id != -1) : (a.libelle_activite.Contains(searchText)))
&& ((a.date_begin_display.HasValue ? DateTime.Now >= a.date_begin_display.Value : a.id != -1) &&
(a.date_end_display.HasValue ? DateTime.Now <= a.date_end_display.Value : a.id != -1))
&& (a.langue == "DUTCH" || a.langue == "B")).ToList<agenda>(); //GET DUTCH
}
You can add multiple where clauses:
var query = dc.agenda;
if(!String.IsNullOrEmpty(group))
query = query.Where(a => a.no_group == group)
if(!activityType.Equals("ALL"))
query = query.Where(a => a.type_manifestation == activityType)
// and so on for all your conditions...
if (Language == ConfBouwHelper.LanguageEnum.French)
query = query.Where(a => (a.langue == "FRENCH" || a.langue == "B"));
else
query = query.Where(a => (a.langue == "DUTCH" || a.langue == "B"));
listAgendaItems = query.ToList<agenda>();
This is a lot cleaner and readable and also solves the problem with the duplication because of the languages.

Categories