简体   繁体   中英

Conditional statement inside WHERE clause

Consider the following records:记录

The outlined records are the same gameobject but they have different configuration names (the record where ConfigurationName is null is a copy from my original table).

string customName= "Config1";

string draftName = "Draft_" + id;

   var list = db.JoinedGameObjects
                            .Where(x => x.TBUploads_ID == id)
                            .Where(x => x.ConfigurationName.Equals(customName) || x.ConfigurationName == null)
                            .Select(x => new GameObjectViewModel()
                            {
                                ID = x.ID,
                                GameObjectName = x.GameObjectName,
                                Category = x.Category,
                                Family = x.Family,
                                Type = x.Type,
                                MetaTypeJson = x.MetaTypeJson,
                                MaterialsJson = x.MaterialsJson,
                                MetadataVisibilityJson = x.MetadataVisibilityJson,
                                Visible = x.joined_Visible ?? x.Visible
                            }).ToList();

This code gets all records where ConfigurationName equals my customName variable or where ConfigurationName = null. However, this is not what I want.

I'll try to explain the desired outcome with an example.

I first want to check if there is a record with this ID with ConfigurationName equal to draftName (if true select this record), if not check if there is a record with this ID with ConfigurationName equal to customName (if true select this gameobject), if not take the record with this ID where ConfigurationName is null.

Can someone guide me in the right direction or provide a working example?

It seems like you already have a working concept, just add a draftName comparison to your Where condition.

It will apply conditions in order defined in Where function

.Where(x => x.ConfigurationName.Equals(draftName) || x.ConfigurationName.Equals(customName) || x.ConfigurationName == null)

Edit #1:

This should reflect what the OP wants.

For the sake of brevity, I used custom `Person` class and simplified the code example, so the concept should be easy to understand:
 public class Person { public int Id { get; set; } public string Name { get; set; } }

Then the rest:

 var people = new List<Person> { new Person {Id= 1, Name = null}, new Person {Id= 1, Name = "Mike"}, new Person {Id= 1, Name = "John"}, new Person{Id = 2, Name= null}, new Person{Id = 2, Name= "Peter"}, }; //filter values string filter1 = "Mike"; string filter2 = "John"; string filter3 = null; var byPriority = people.Where(p=> p.Name == filter1 || p.Name == filter2 || p.Name == filter3).OrderByDescending(p => p.Name == filter1).ThenByDescending(p => p.Name == filter2).ThenByDescending(p => p.Name == filter3); var oneByIId = byPriority.GroupBy(p => p.Id).Select(g => g.First()).ToList();

The key here is to order the records by conditions priority. Then group the records and take the first one from each group

Edit #2

In some cases, LINQ to SQL may fail when comparing to `NULL`, in such a case, modify null comparing parts as this:
 var byPriority = people.Where(p=> p.Name == filter1 || p.Name == filter2 || object.Equals(p.Name,filter3)).OrderByDescending(p => p.Name == filter1).ThenByDescending(p => p.Name == filter2).ThenByDescending(p => object.Equals(p.Name,filter3)); var oneByIId = byPriority.GroupBy(p => p.Id).Select(g => g.First()).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM