U
U
UrbanRider2012-12-02 10:13:10
SQL
UrbanRider, 2012-12-02 10:13:10

Querying data from a database with multiple conditions

Good afternoon, dear% username%.
There is a C# class with the following content:

namespace Dzen_volunteer
{
    public class Volunteer : DataContext
    {
        public Table<Person> Persons;
        public Volunteer(string connection) : base(connection) { }
    }

    [Table(Name = "Persons")]
    public class Person
    {
        string _guid;
        [Column(IsPrimaryKey = true, Storage = "_guid")]
        public string Guid
        {
            get
            {
                return this._guid;
            }
            set
            {
                this._guid = value;
            }
...куча полей...
        }
    }
}


To get a complete list of records from the database (similar to SELECT ALL) I do the following:

foreach (Person pers in db.Persons)
{
this.listPersons.Add(pers);
this.personList.Items.Add(pers.FirstName + " " + pers.FatherName + " " + pers.LastName);
}


Please tell me how to correctly select from the database for several fields at once, is it possible to simply search for the equivalent of the Person class in the database?
For example, somehow we set several fields to an instance of the person class and instead of writing a bunch:

where pers.Field==""


write something like:

where pers equals person


Immediately I apologize for the govnokod. First experience with linq to sql. Studied on msdn.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
U
UrbanRider, 2012-12-03
@UrbanRider

If anyone else is interested, then everything is solved quite simply, as it turned out.
You should use a where construct similar to the following:

where pers.FirstName == (AddForm.Person.FirstName == "" ? pers.FirstName : AddForm.Person.FirstName) && pers.LastName==(AddForm.Person.LastName=="" ? pers.LastName:AddForm.Person.LastName) ... остальные условия...

E
eforce, 2012-12-02
@eforce

As far as I understand, you need to make a selection on an array of values ​​of some field, then you need to use Contains , which will create a WHERE IN construct in SQL. See this link for an example: Linq-To-Sql: Alternative to the 'WHERE IN' Expression

S
Seter17, 2012-12-02
@Seter17

hmm, I didn't really understand what you want, but judging by what was written above, maybe this will help?
Dynamic LINQ

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question