简体   繁体   中英

How to put where condition in select condition in Linq query

添加MickaëlDerriey的代码后。我收到编译错误。你能告诉我什么是错的吗? ? I have an issue. Here i want to filter based on some condition for Fields and Datafield . How can i do that?

Below is my linq query: Note: This piece of code is working.

 DataSet queries = query.GetQueryDetails(reportName);
 str = queries.Tables[1].AsEnumerable().Select(dataRow => new Query { CommandText = dataRow.Field<string>("CommandText"), DataSetName = dataRow.Field<string>("DataSetName"), Key = dataRow.Field<int>("Fields"), Value=dataRow.Field<string>("DataField") }).ToList();

And i want to do something like this, but it's not possible.: Note: Below code is not working. showing compile error.

 DataSet queries = query.GetQueryDetails(reportName);
 str = queries.Tables[1].AsEnumerable().Select(dataRow => new Query { CommandText = dataRow.Field<string>("CommandText"), DataSetName = dataRow.Field<string>("DataSetName"), Key = dataRow.Field<int>("Fields").Where(dataRow.Field<int>(("DictVal") == "Key")), Value=dataRow.Field<string>("DataField").Where(dataRow.Field<string>(("DictVal") == "Value")) }).ToList();

I know we cannot put where condition wherever we want. But i want to select Fields and DataField columns based on condition. Can anyone please help me? Is it possible?

Just for reference:

public partial class Query
{
    public string DataSetName { get; set; }
    public string CommandText { get; set; }
    public int Key { get; set; }
    public string Value { get; set; }
}

Note: CommandText, DataSetName, Fields and DictVal are the columns of the table.

welcome to StackOverflow 👋!

I'd go with something like:

str = queries
    .Tables[1]
    .AsEnumerable()
    .Select(dataRow =>
    {
        var query = new Query
        {
            CommandText = dataRow.Field<string>("CommandText"),
            DataSetName = dataRow.Field<string>("DataSetName")
        };

        switch (dataRow.Field<string>("DictVal"))
        {
            case "Key":
                query.Key = dataRow.Field<int>("Fields");
                break;

            case "Value":
                query.Value = dataRow.Field<string>("DataField");
                break;
        }

        return query;
    })
    .ToList();

This uses the other notation for lamda expressions where they declare a full body.

What compile error did you get?

Following is my answer:

str = queries.Tables[1]
    .AsEnumerable()
    .Select(dataRow =>
    {
        var dictVal = dataRow.Field<string>("DictVal");
        return new Query
        {
            CommandText = dataRow.Field<string>("CommandText"), 
            DataSetName = dataRow.Field<string>("DataSetName"),
            Key = dictVal == "Key" 
                ? dataRow.Field<int>("Fields")
                : int.MinValue, 
            Value = dictVal == "Value"
                ? dataRow.Field<string>("DataField") 
                : string.Empty
        };
    });

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